File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about Singleton Pattern

 
siva prakash
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Nikhil Vasaikar
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
steve souza
Ranch Hand
Posts: 862
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic