wood burning stoves*
The moose likes Java in General and the fly likes Use of Singleton Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Use of Singleton Classes" Watch "Use of Singleton Classes" New topic
Author

Use of Singleton Classes

velmurugan
Greenhorn

Joined: Jun 08, 2006
Posts: 24
Hi

Everybody knows about the Singleton Classes, Interviewer asked me what is the use of Singleton Classes Why we need that? I failed to anser that question.

Please anyone help me What is the use of Signleton with any realtime example
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
This may help.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Singleton Pattern is a utility class of which only one instance is allowed in memory at run time. If you look in any of the project you will find lot of use of Singleton Pattern.


Some common examples using Singleton Implementation are
1) Database Connection Factory
2) XMLParser Class
3) Printer Class

HTH,


~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
The typesafe Enum pattern implemented in Java 1.5 is a specialization of the Singleton (each element of the Enumeration has exactly ONE instance).

So there's one application.

- Adam
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Java's Runtime class is a singleton class. Singletoness is basically a property of the system.

You can write a logger as a singleton or service locator as a singleton which is locating certain service like EJBHome from jndi server.

Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Be careful not to jump too quickly to Singleton as a solution. It's often not really necessary, and opens a whole can of design problem worms. Scroll down to the UML, OO etc. forum and search for Singleton for some discussions. It might liven up your next interview to be able to talk about how to make them, and why not to.
[ July 31, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Yes Stan I agree.

I really liked the approach given by you, Frank and Ilja.

Naseem
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
I am not sure but i think this design pattern is used in most of the windows OS interface..

for example if you open a task manager you can just open single taskmanger that means it uses single instance for opening only one taskmanager if you try to open another taskmanger it wont opens new taskmanger

in the same way windows desktop is also one

and notepad is also one


sorry if i am wrong

regards
cinux


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by saikrishna cinux:
I am not sure but i think this design pattern is used in most of the windows OS interface..


I hope you will agree that this doesn't necessarily mean that it is a good design pattern It has it's uses, but it shouldn't be over-used, and quite often it is.


for example if you open a task manager you can just open single taskmanger that means it uses single instance for opening only one taskmanager if you try to open another taskmanger it wont opens new taskmanger

in the same way windows desktop is also one


I guess, in concept, that makes them singletons. However, having not seen the code for the task manager of the desktop, I would be hesitant to claim that it follows the singleton design pattern in the way it is typically implemented. It is possible that the "singleton-ness" of these applications is controlled by the operating system, rather than being enforced at the class level.


and notepad is also one


I'm not so sure about this one. I seem to be able to open multiple notepad windows at the same time.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of Singleton Classes
 
Similar Threads
what are singleton classes in Java API
Instantiating a class with private constructor
Singleton classes in Java
java(intermediate)
utility of singleton classes