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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris
  • Mark post as helpful
  • send pies
  • 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)
  • Use your singletons wisely (J.B. Rainsberger)
  • When is a Singleton not a Singleton? (Joshua Fox)
  • Patterns I Hate #1: Singleton (Alex Miller)
  • 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
     
    Attractive, successful people love this tiny ad:
    Java file APIs (DOC, XLS, PDF, and many more)
    https://products.aspose.com/total/java
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!