• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

When do we need to use Singleton?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone give me a real example of using this pattern in practice? I know it's widely used, but is it sometimes somehow misused? When do we actaully want a class to have only one instance?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to recommend a situation where Singleton is the best choice. Singleton should only be used where its benefits (it allows client code to pretend to create multiple instances but secretly enforces a limit on number of instances, and provides a globally visible access point to do so) outweigh its many limitations (it's hard to test, inflexible and hard to dynamically configure, spreads the knowledge that it is a Singleton throughout the code, and embeds a potentially volatile business decision deep into the structure of the code.)

For these reasons, a lot of people here would recommend that the Singleton pattern should hardly ever be used. Almost all occasions where a Singleton might seem a reasonable choice would likely be better served by "just create one" and pass it around as needed.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To elaborate on what Frank wrote, even when we only want to have one instance of a class in a system, what we still typically don't want is

- having that class know that there only exists one instance in the system

- having all clients of the class know where the instance they should use is originally coming from.

I've come to the conclusion that actually *most* uses of the Singleton patterns are misuses. In the system I'm working on - which is under development for nearly a decade now and consists of roughly a million lines of code - there are only two places where we used something "singleton-like" and it didn't hurt us: we have a global access point for getting a logger, and we have a global access point to determine whether the system is running in development- or production-mode (the latter makes it possible to disable non-production-ready features in production-mode).
 
Mingwei Jiang
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Frank and Ilja for your replies.

Yes, I'm always wandering on making the class singleton. Sometimes I did, but later I found it is really not necessary to do so, and changed it to the "just create one" solution.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic