Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Singleton Classes

 
velmurugan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help.
 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Stan I agree.

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

Naseem
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic