Win a copy of Rust Web Development this week in the Other Languages forum!
    Bookmark Topic Watch 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Not the most important topic, but many questions relate to the singleton pattern. It's a deceptively simple pattern with subtle problems (to the point that some call it an AntiPattern). Forum regulars often suggest that you Just Create One and use Dependency Injection to get it into the system.




A singleton is a class of which there cannot be multiple instances. It is one of the GangOfFour DesignPatterns.

In contrast to a class with all static methods (often called helper class), the singleton pattern encapsulates the knowledge of how many instances and of which actual class are used, thereby providing the option of making use of polymorphism. That is, you can change the implementation used for the Singleton based on some external stimulus (property file or something) without the clients even noticing.

Related Forum Threads:

  • Google:site:coderanch.com+singleton


  • Other Resources:

  • What is a Singleton (Frank Carver)
  • Wiki:SingletonPattern
  • Singleton vs. Just Create One (Robert C. Martin)
  • Singleton and Monostate (pdf) (Robert C. Martin)WaybackMachine
  • Singleton and Monostate (pdf) (Robert C. Martin)(University of Turku, Finland)
  • Use your singletons wisely (J.B. Rainsberger)(Original: WaybackMachine)
  • Use your singletons wisely (J.B. Rainsberger)(Updates 10 and 17 years later by original author!)
  • Singleton Pattern Deep Dive: Why is it bad? (Sihui Huang)(Blog Post from 2020)
  • When is a Singleton not a Singleton? (Joshua Fox)
  • Updated wisdom on how some of the issues mentioned in the link above play out in Modern Java (Alex Miller)
  • Patterns I Hate #1: Singleton (Alex Miller)
  • From the author of Testability Explorer--Several Linked Articles About the Disadvantages of Singletons (Misko Hevery)
  • Singleton considered stupid (Steve Yegge)




  • If you are thinking of using the singleton pattern, resist with all your might. It is one of the easiest DesignPatterns to understand, probably the most popular, and definitely the most abused. Use a singleton only if there is a fundamental reason inherent to your problem or your design why there can only be one instance of your class. "I need only one instance in this application" is not a fundamental reason: using a singleton will unnecessary constrain the evolution and reuse of your code, and also makes it harder to understand because you're giving your reader the wrong signals. "I need easy access to shared data using a static method" is also not a valid reason: an InversionOfControl framework such as the SpringFramework or PicoContainer is a much better way to go about that. -- PeterDenHaan
     
    Consider Paul's rocket mass heater.
      Bookmark Topic Watch Topic
    • New Topic