Women in Technology

Hear us Roar



Article:
  Using the Singleton Pattern
Subject:   Singleton Considered Harmful
Date:   2003-08-28 11:09:01
From:   jimothy
Aside from often being overused as a substitute for global variables or functions (static methods/variables or a better design should eliminate this need), a potential problem with the Singleton pattern is it makes the class determine how it will be used.


The author's example of the find dialog demonstrates my point well. While Word may only want to display one find dialog, Word (or the code that responds to the Find menu select or Ctrl-F event) should be responsible for enforcing this, not the find dialog itself. The reusability of the Find dialog is limited because its internal enforcement of the number of instances is inappropriate. The same is true of the SingletonFrame example.


While the StringManager class may be a more appropriate use of the Singleton pattern than the SingletonFrame, it too may be inappropriate. An alternative, and arguably better, solution may be the use of the Factory pattern. A StringManagerFactory can create instances of StringManager, or return a cached copy when appropriate. This is more flexible, because it allows client code, not StringManager, to determine how many StringManagers are necessary to accomplish a task, and because the Factory can return subclasses of StringManager should this ever be necessary.


Finally, to ease unit testing, it is often best to pass an instance of StringManager to the methods or classes that use the StringManager, rather than have those classes or methods get an instance from either StringManager or StringManagerFactory. The reason for this is you can pass a StringManager instance configured appropriately for your unit test, or use a mock-object subclass of StringManager.

Full Threads Oldest First

Showing messages 1 through 4 of 4.

  • Singleton Considered Harmful
    2003-08-29 05:15:05  diegof79 [View]

    I agree with your comment, for more information on why the singleton pattern is considered harmful see:

    http://www.c2.com/cgi/wiki?SingletonsAreEvil
    http://www.c2.com/cgi/wiki?AnotherJavaSingletonProblem
    • Singleton Considered Harmful
      2003-09-09 00:20:22  anonymous2 [View]

      hi,

      From an enterprise application point of view,
      There are often services which are deployed as singletons. For eg, JNDIlookUpDirectory of the application.

      Only one instance manages the addresses in the directory.

      The problem wiyth singleton, classes, is that they cannot be deployed in a clustered environment.

      If deployed, only one instance of the clsuter will have this singletton class instantiated else it will fail.

      In cases, where scalablity and failover is desired, singleton pattern offers a bottleneck.

      Is there any work around for such singletons, already existing in this existing application.

      Regards,
      Dimple
    • Singleton Considered Harmful
      2003-08-29 11:37:24  jimothy [View]

      Thanks for the links. The first link (SingletonsAreEvil) linked me to an IBM developerWorks article, which it turns out I had previously read before, and formed much of the basis for what I wrote in my post.

      It's a good article that explains the pitfalls of singletons, how to structure your code to avoid using singletons, and the benefits of doing so. I recommend everyone who read this O'Reilly article also take a look at a counterpoint: http://www-106.ibm.com/developerworks/webservices/library/co-single.html

      My advice is not to use any pattern blindly; the GoF describes the appropriate contexts and forces for using a particular pattern. The Singleton pattern is especially easy to abuse, because it is so simple conceptually. But no pattern should be taken as the Gospel.
  • Singleton Considered Harmful
    2003-08-29 05:14:58  diegof79 [View]

    I agree with you comment, for more information on why the singleton pattern is considered harmful see:

    http://www.c2.com/cgi/wiki?SingletonsAreEvil
    http://www.c2.com/cgi/wiki?AnotherJavaSingletonProblem