• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about Singleton Pattern

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ,


thanks,

bye
siva
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
Single@3ac748
Reusing Instance
Single@3ac748

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 ]
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...

Thanks
Maulin
 
reply
    Bookmark Topic Watch Topic
  • New Topic