File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes OO, Patterns, UML and Refactoring and the fly likes about Singleton Pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "about Singleton Pattern" Watch "about Singleton Pattern" New topic

about Singleton Pattern

siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
hi friends ,

can any tell when ( ie at what situation ) one can use Sigleton Pattern in designing a class , what are its advantages and disavantages

hoping for reply ,


Nikhil Vasaikar
Ranch Hand

Joined: Aug 18, 2004
Posts: 56
Hi Siva,

A Singleton is used when you want to make sure that there is only one instance of your class present with a global point of access. The same instance is used to serve diferent requests. Please refer to the example below.

The Output -
Creating Instance...
Reusing Instance

Notice that the constructor of class Single is private. Also it provides a public static method to create an object of class Single. Singletons can be used for ConcreteFactories which return a family of objects.
The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance i.e the static method used in our example needs to change.
[ August 24, 2004: Message edited by: Nikhil Vasaikar ]
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 862
I'd also add that lazy initialization is overused in the singleton pattern and I would opt for initializing the static instance directly unless you have a compelling reason not to. - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've just been frustrated by a vendor package with a few static and singleton classes that were not designed for customization. The getInstance method on the class may burn you one day, too. Consider a nice utilitiy package you pick up somewhere with SingletonClass.getInstance(). After a while you decide you'd like to subclass SingletonClass with CustomSingleton and override a couple methods. But many places in your code you have calls to SingletonClass.getInstance(). Ouch.

If Singleton still seems to be your answer, maybe it should implement an interface so clients can be coded:

SingletonInterface x = ConfigurableFactory.getSingleton();

and some configuration file

singletonclass = com.mycompany.myproject.CustomSingleton

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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Stan,

Thats exactly what I do.

I create Service Interface, Impl as Singleton and use Factory to specify that ServiceNameX has ImplX or ImplY or something...

I agree. Here's the link:
subject: about Singleton Pattern
It's not a secret anymore!