permaculture playing cards*
The moose likes OO, Patterns, UML and Refactoring and the fly likes When would it be imperative to use Singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "When would it be imperative to use Singleton" Watch "When would it be imperative to use Singleton" New topic
Author

When would it be imperative to use Singleton

manish ahuja
Ranch Hand

Joined: Oct 23, 2003
Posts: 312
Hi All


When would it be imperative to use the Singleton design pattern. Can someone just list a few real life scenarios where Singleton had to be there


Rgrds
Manish
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I have honestly not met such a situation where the use of the SingletonPattern would have been required.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Singleton usage is best suited for situations when you need to have full control on the number of instances of a class that are available at a specified moment.
For more details you can consult GoF. Martin Fowler talks a little about it too.

./pope


blog - InfoQ.com
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Lasse Koskela:
I have honestly not met such a situation where the use of the SingletonPattern would have been required.


Hibernate Session management inside a container is a very good example. I will update the post when I will find out the real link
Here it is (I should stop head banging)

./pope
[ November 18, 2004: Message edited by: Ali Pope ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Ali Pope:
Hibernate Session management inside a container is a very good example.
True. Although it's possible to just create a new SessionFactory every time someone needs one, it would be a performance killer to do so.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Caches, pools and stateful interfaces (connections) to other systems are a couple examples I use. Servlets are singletons to avoid the overhead of creating and garbage collecting huge numbers of instances. (Anybody know if the theory is backed up by performance measurements?)
[ November 18, 2004: 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
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Singleton Pattern is mostly useful when your real-life entity is a singleton.

For example, you have a multi-threaded app that communicates with an external server. In this case, it's better to have a Singleton class that handles the communication with the server. Not only do you put all your communication logic in one place, but you can also control the access to the server. For example, the Singleton can pool connections to the server, control the number of concurrent connections to the server etc, etc
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

I too use singletons for caching. However it is more for performance than being absolutely imperative.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
"Any Class whose state does not change may also qualify as a Singleton",
The above point also may be considered for the usage of singleton.
[ November 18, 2004: Message edited by: Sujatha Kumar ]

SCJP,SCWCD,SCBCD,SCEA Part I
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jayesh Lalwani:
Singleton Pattern is mostly useful when your real-life entity is a singleton.

For example, you have a multi-threaded app that communicates with an external server.


That's actually one of the examples where I find using a Singleton to be a suboptimal solution. (I just picked it because it was the last in this thread - I'm not convinced by the other examples, either.)

The reason is that the "only one server" requirement is likely more of a temporary coincidence than an unchanging fact of life.

The problem actually is not the "guarantee only one instance" part of the Singletons' definition, but the global access point to that one instance.

When the business people after a year of development find out that now they'd actually prefer to have two servers, your whole code is likely to be build on the premise that there is only one, and that they can access it through the static methods of the Singleton. It's going to become a refactoring nightmare.

That's why I would prefer the Just Create One pattern - simply create the one instance of the server at a central place and *pass that instance around*, so that all the other objects don't know where it's coming from and how much of them might exist. As a nice side effect, that will also make unit testing your code much easier.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Stan James:
Servlets are singletons to avoid the overhead of creating and garbage collecting huge numbers of instances.


Servlets aren't Singletons, but Just Create One's, aren't they?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yeah, Just Create One is a beter description. I never drew the distinction that Singleton required a software solution that forced a single instance. But the GoF intent and implementation sections both say "to ensure a class only has one instance" so I guess I needed a refresher.

GoF implementation also has a neat section on subclassing singletons. Quite a few discussions say the inability to subclass is a drawback to singleton. Refreshers all around, bartender!
[ November 20, 2004: Message edited by: Stan James ]
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Uncle Bob recently ranted against the use of Singletons.
[ November 21, 2004: Message edited by: Pho Tek ]

Regards,

Pho
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Pho Tek:
Uncle Bob recently ranted against the use of Singletons.


And I think he has some very good points...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When would it be imperative to use Singleton
 
Similar Threads
Does service locator needs to be a Singleton to cache
Thread-safe lazy Singleton creation
Double Checked Locking
why stateful
Singleton Class ?