• 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

when to use the Singleton design pattern ?

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to think of situations when one might want to use the Singleton pattern.
Its intent is to ensure that a class has only one instance, and to provide a global point of access to it.

Can you give me a few examples on when to use it ?
Thanks
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me a few examples on when to use it ?

My general answer is "use it as rarely as possible". Singleton has so many drawbacks, many of them not immediately obvious, that using it is almost always a mistake. Search for Singleton in this forum to read some of the many discussions on this topic. In the great majority of cases, Singleton can straight-forwardly be replaced by creating a single instance and passing it around where needed ("dependency injection" often helps here).

Cases where Singleton might be appropriate include difficult cases such as accessing your external code inside a particularly constrained callback from opaque legacy software. But even then, if you can do it another way, the other way is probably better.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a singleton when you only need to have one instance running.

Also be sure that one, and only one instance will be created, and that peculiar classloader configurations and application packaging won't actually cause multiple instances to be created.

A singleton pattern is only the best pattern to use when it is better than all of the other alternatives.

Cheers!

-Cameron McKenzie
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cameron McKenzie Use a singleton when you only need to have one instance running.

I guess I'd phrase that even more strongly. Something more like:

Only use a Singleton when there must only, ever, be one instance.

Turning a regular class into a Singleton is extra code and extra effort. If you are not sure if there might one day be a need for more than one instance, don't go to the extra effort of restricting it to be a Singleton. Leaving your options open by writing less code is a win/win scenario.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cameron W. McKenzie:
Use a singleton when you only need to have one instance running.



If you only need one instance, just create not more than one, that's my advice.

The Singleton pattern does more than ensuring the existance of just one instance:

- it puts that responsibilty inside the class itself (which means that it can't be reused in contexts where more instances would make sense), and

- it provides a global access point to that instance (with all the bad effects that globals have in general)
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic