Which Design Patterns Should Be Retired? (In Defense of Singleton)

Addison-Wesley asked the patterns community (or at least those who were there at the beginning) about their opinion on various issues. This is the second post of what should have been three (though I probably will only get to the first two).

For this very specific question, I expect everyone to say: Retire Singleton!

I beg to differ; for its time, Singleton was right. “Its time” mostly means single-threaded C++ code, as written by 3 of the 4 authors of the Design Patterns book. Today, many will argue that multi-threaded Java programs have made Singleton obsolete and even harmful. In my take at design patterns that would be wrong. Singleton should not perish, it should simply be adapted to its time. What ever thread-local information you may have, some it will be global to that thread. How is that not a thread-specific singleton? Or, take a group of application servers acting as a group: How is the group communication protocol not to provide a global id that makes the group identifiable and provide some global state attached to it? This may not be your granddaddy’s good ol’ singleton, but a singleton it may be nevertheless.

This is also where pattern descriptions will pass (or fail) the test of time: Does the original description of Singleton allow for these variations or not? If not, it may have been too narrow a description. I’d assume then that specific usage examples, tied to the programming languages and systems and technologies of its time, dominated the description and the abstracion process “out of examples, timeless patterns” did not yet conclude.

Beyond Singleton, some may want to retire Visitor, mostly because we find functional programming concepts being added to programming languages more and more, and multiple dispatch (which Visitor is about) comes for free. I say: Simply be precise about the scope. Visitor is (kind of) fine for Java but in a different context may not be needed as a design pattern because it has been cast as a language feature.

Design pattern should evolve and there is nothing wrong with it. John would approve.

A summary of all posts can be found on the InformIT site.

PS: Turned out, not many on the InformIT site were pointing to this issue. Looks like I was beating a dead horse!

Posted on


  1. Michael Richmond Avatar
    Michael Richmond

    Two common uses for singletons is to wrap configuration values (and possibly implicitly parse any associated configuration file), and for logger objects. Admittedly these are specialized cases, but to my aesthetic mind explicitly passing these types of objects into constructors introduces undesirable verbosity. It can also lead to passing a value into a constructor for the sole purpose of passing it further down the calling tree.
    I have encountered plenty of cases where a developer reads the source in front of them, notices that the “config” object is not used locally and so deletes that from the constructor signature leading to hard to find bugs in inherited code. These are primarily implicit dependencies that should not have crept into the code in the first place. But the style of explicitly passing these “service objects” around in my view contributes to the problem.
    Dirk, we might not be in the majority. But I haven’t given up the good fight. 😉

  2. umit yalcinalp Avatar
    umit yalcinalp

    Giovanni, isn’t this akin to whether the services should be stateful or stateless? In my opinion, we are, as an industry, too hung up on mutually exclusive states, either or to the extent of being a bit religious. Both have its uses. Persistence is also another area where it has been divided in terms of its patterns and solutions, in the end there are multiple ways of solving the problem. Nowadays, I teach my students multiple ways of solving a problem along with pros and cons, instead of abolishing one way over another.

    1. Giovanni Avatar

      Umit, stateful or stateless services are irrelevant in the context of my previous comment—the intent of the singleton is about guaranteeing the existence of a single instance of a class.
      My point is that, in practice, the drawbacks are so bad that make the singleton not worth using—hidden dependencies in the codebase are a major source of headaches as far as maintainability, testability and usability are concerned.
      I’ve come to this conclusion by making the mistake of writing singletons and suffering the consequences myself in the past (and by having to maintain / fix code written by others as well).
      Passing parameters from above, in my experience, is a much better solution to the problem the singleton tries to solve (Kevlin Henney has written about this as well http://accu.org/index.php/journals/1411).

      1. Dirk Riehle Avatar

        Multithreading was the issue in going from C++ to Java, and testability is the issue with the growth of test-driven development and continuous deployment.
        My point is that no pattern was meant to be interpreted in a super-narrow fashion. For example, I think a Singleton means to have one (typically global) instance of one particular service because that object is always just one object whenever you run your software, whether for testing or for operations. Object pools are a design alternative that may be more suitable under different circumstances.
        So, if you program to an implementation (i.e. one particular class) rather than a service interface, you make your code hard to test. If you program towards a service interface, it is easy to configure your code with mocks or test-specific implementations of the class. You still have one global instance, it is just of a different implementation class, depending on whether you run your code for testing, operations, or whatever.

        1. Giovanni Avatar

          If I understand your point correctly, you are saying that being a singleton refers only to the number of instances in the system, not to the way those instances were created. If that’s the case (please confirm I’m understanding correctly), I think we can probably agree on many other aspects.
          What I was referring to was my understanding of the pattern—which, in my experience, matches the way most other people understand it—i.e., the only instance is always accessed by calling a static method (e.g., Singleton::instance() ) which also creates the instance if necessary (as shown in the GOF book). That is the source of all the major problems I have with the pattern—e.g., invisible dependencies, difficulty in testing—and the source of many discussions and articles on how to make the creation thread safe (I don’t remember any specific articles on the singleton where the major point was the thread safety of the instance itself).
          Summing up, while I don’t have a problem with a single instance of a service passed around explicitly to the parts of the system that need it, I have an issue with hiding that dependency behind a function (or static method) that can be called in random places in the code.

          1. Dirk Riehle Avatar

            These are the aspects at play that I see:
            1. one object serving as a resource gatekeeper
            2. global centralized access to that one object
            3. how the class of that object is defined
            Singleton simply shows you how to get 1 + 2 and what that is good for. It does not tell you 3, how to create the object. That is left IMO to any of the many object creation patterns.
            So looks like we differ on 2, whether global and easy access to the object is good or bad. You seem to want to pass the object along all the time? Given the number of singletons that traditional systems have or may have, aren’t you passing the universe along in each method call?

          2. Giovanni Avatar

            The object can be passed along in a constructor along with the other dependencies, or in a method call. The choice depends very much on the context. Usually, I like to build the system bottom up in the main, and I solve all the static dependencies there.
            My experience is that the number of singletons (or other dependencies, for that matter) an object depends on is not that high—unless we are dealing with a pathologically bad system, which, in my experience, are not that common (and I’ve seen quite a few really bad ones). So, in practice there will be only a very small slice of the universe to be passed along.
            Not stating the dependency explicitly, but having a call to a static method to access the singleton in the guts of the code doesn’t solve any problems—the dependency is still there, and it will affect everything that needs to be done for testing, threading, etc.
            If making the dependency explicit makes the code look bad, it is a strong sign that something needs to change in the design. Hiding that dependency, in my opinion, is akin to make a room look clean by hiding the dust under the carpet—it will look clean on the surface, but the bugs will prosper unseen.
            In my experience as a developer, singletons are one of the worst source of bugs and maintenance headaches, and, as I wrote before, I haven’t seen a single case in which its usage made the code better in any way.

  3. Giovanni Avatar

    I think the real issue with the singleton has nothing to do with multithreading.
    The main problem with it is the fact that dependencies on it are hidden in the code instead of being declared explicitly, which makes testing and usability of the code much harder (not to talk about the tendencies of many singletons to become multi-tons at some point of their life).
    In my experience, I haven’t found a single use of singleton that couldn’t be replaced with passing parameters to a method or dependencies via a constructor.

  4. Dirk Riehle Avatar

    Michael, I’m right there with you, but it was not my impression that we are in the majority…

  5. Michael Richmond Avatar

    But design patterns should not be language specific.
    “Today, many will argue that multi-threaded Java programs have made Singleton obsolete and even harmful.”
    Is this quote intended to convey “Today, many Java developers will argue that multi-threaded Java programs have made Singleton obsolete and even harmful.”?
    Singleton still has great value in Java, other more traditional languages, and even languages that have more modern features. I would agree that there are fewer instances where the Singleton pattern is necessary today compared to when the GoF book was published. But we are a long way from de-listing the pattern.
    It’s not even clear to me that de-listing is a good idea. De-emphasizing might make sense. But part of the goal of the patterns movement is to capture good macro design in a form that explains the forces and provides solid re-usable implementation approaches that are easy to consume.
    Current commonly used development practice may have shifted, but the pattern and the scenarios where it is useful are still with us.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share the Joy

Share on LinkedIn

Share by email

Share on Twitter / X

Share on WhatsApp

Featured Startups

QDAcity makes qualitative research and qualitative data analysis fun and easy.
EDITIVE makes inter- and intra-company document collaboration more effective.

Featured Projects

Making free and open data easy, safe, and reliable to use
Bringing business intelligence to engineering management
Making open source in products easy, safe, and fun to use