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
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
posted
0
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
posted
0
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.
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
posted
0
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.