Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to use the Singleton design pattern ?

 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • 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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 6920
  • Mark post as helpful
  • send pies
  • 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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • 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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic