2147

The singleton pattern is a fully paid up member of the GoF's patterns book, but it lately seems rather orphaned by the developer world. I still use quite a lot of singletons, especially for factory classes, and while you have to be a bit careful about multithreading issues (like any class actually), I fail to see why they are so awful.

Stack Overflow especially seems to assume that everyone agrees that Singletons are evil. Why?

Please support your answers with "facts, references, or specific expertise"

24
  • 7
    I have to say that using a singleton design has burned me recently as I have tried to adapt the code. As I do it in my free time, I'm almost too lazy to refactor this. Bad news for productivity.
    – Marcin
    Oct 10, 2008 at 18:49
  • 81
    There's a lot of 'cons' in the answers, but I'd also like to see some good examples of when the pattern is good, to contrast with the bad...
    – DGM
    Oct 15, 2008 at 6:03
  • 51
    I wrote a blog post on the subject a few months ago: jalf.dk/blog/2010/03/… -- and let me just say it outright. I can't personally think of a single situation where a singleton is the right solution. That doesn't mean such a situation does not exist, but... calling them rare is an understatement.
    – jalf
    Jun 25, 2010 at 2:12
  • 9
    @AdamSmith it doesn't mean you have to, but it means you can access it like that. And if you don't intend to access it like that, then there's little reason to make it a singleton in the first place. So your argument is effectively "there's no harm in making a singleton if we don't treat it as a singleton. Yeah, great. My car also doesn't pollute if I don't drive in it. But then it's easier to just not acquire a car in the first place. ;) (full disclosure: I don't actually have a car)
    – jalf
    May 26, 2013 at 9:55
  • 51
    The worst part of this whole topic is that the people who hate singletons rarely give concrete suggestions for what to use instead. The links to journal articles and self-published blogs all through this SO article, for example, go on and on about why not to use singletons (and they're all excellent reasons), but they're extremely slim on replacements. Lots of handwaving, though. Those of us trying to teach new programmers why not to use singletons don't have many good third-party counterexamples to point to, only contrived examples. It's wearying.
    – Ti Strga
    Sep 8, 2015 at 18:32

36 Answers 36

1
2
4

A pattern emerges when several people (or teams) arrives at similar or identical solutions. A lot of people still use singletons in their original form or using factory templates (good discussion in Alexandrescu's Modern C++ Design). Concurrency and difficulty in managing the lifetime of the object are the main obstacles, with the former easily managed as you suggest.

Like all choices, Singleton has its fair share of ups and downs. I think they can be used in moderation, especially for objects that survive the application life span. The fact that they resemble (and probably are) globals have presumably set off the purists.

4

Some counterpoints from the author:

You are stuck if you need to make the class not single in the future Not at all - I was in this situation with a single database connection singleton that I wanted to turn into a connection pool. Remember that every singleton is accessed through a standard method:

MyClass.instance

This is similar to the signature of a factory method. All I did was update the instance method to return the next connection from the pool - no other changes required. That would have been far harder if we had NOT been using a singleton.

Singletons are just fancy globals Can't argue with that but so are all static fields and methods - anything that is accessed from the class rather than an instance is essentially global and I dont see so much pushback on the use of static fields?

Not saying that Singletons are good, just pushing back at some of the 'conventional wisdom' here.

3
  • 2
    no, you're missing the point. In the first point, I still don't have the option to create another instance. I can wish and hope and pray that the next time I call getInstance(), it'll give me a different instance, but I still have no way to just say "I've got one instance. Now I need a second instance for another task". On the second point, many things are just fancy globals, yes, but singletons couple this with a lot of unwanted baggage (the 1-instance restriction, the complex and error-prone synchronization issues), for example.
    – jalf
    Nov 10, 2010 at 14:34
  • 1
    @Ewan: Mutable static fields are evil, for many of the reasons that Singletons are. If you haven't seen the pushback against them, you haven't been looking hard enough; static is just the C#/Java spelling for "global". :) As for static functions, they're a bit less of an issue without static variables; the biggest problem with globalness is the "action at a distance" and hidden dependencies that it brings about, and a function that has no inter-call storage space (no instance, and no mutable static variables) ends up quite limited in what it can depend on (and what it can mangle).
    – cHao
    Feb 13, 2013 at 15:58
  • 1
    "Not at all - I was in this situation with a single database connection singleton that I wanted to turn into a connection pool." You still wanted this one class to be single. It changed from single connection to single connection pool. You would be screwed if you wanted to split your database server into two independent. Now your connection pool would need to know to which server to connect. Without singletons in the first place, the only change in your DIC would be to create a second DB connection object with the second servers details and inject it into all objects that moved to this DB.
    – Sven
    May 17, 2013 at 19:52
3

This is what I think is missing from the answers so far:

If you need one instance of this object per process address space (and you are as confident as you can be that this requirement will not change), you should make it a singleton.

Otherwise, it's not a singleton.

This is a very odd requirement, hardly ever of interest to the user. Processes and address space isolation are an implementation detail. They only impact on the user when they want to stop your application using kill or Task Manager.

Apart from building a caching system, there aren't that many reasons why you'd be so certain that there should only be on instance of something per process. How about a logging system? Might be better for that to be per-thread or more fine-grained so you can trace the origin of messages more automatically. How about the application's main window? It depends; maybe you'll want all the user's documents to be managed by the same process for some reason, in which case there would be multiple "main windows" in that process.

0
3

The Singleton – the anti-pattern! by Mark Radford (Overload Journal #57 – Oct 2003) is a good read about why Singleton is regarded an anti-pattern. The article also discusses two alternatives design approaches for replacing Singleton.

2
  • Frank The link doesn't appear to work?
    – user18443
    Oct 15, 2008 at 11:54
  • tdyen, sorry for late reply! The link works fine for me (just tried it with Opera and Firefox)... Oct 20, 2008 at 13:42
2

It blurs the separation of concerns.

Supposed that you have a singleton, you can call this instance from anywhere inside your class. Your class is no longer as pure as it should be. Your class will now no longer operate on its members and the members that it receives explicitly. This will create confusion, because the users of the class don't know what is the sufficient information the class needs. The whole idea of encapsulation is to hide the how of a method from the users, but if a singleton is used inside the method, one will have to know the state of the singleton in order to use the method correctly. This is anti-OOP.

1
  • 5
    Not quite sure about this answer. The same could be said for any object really. The real problem is that a Singleton can be accessed from ANYWHERE. A clean API and sufficient documentation can keep clients from using it incorrectly.
    – Tim Frey
    Sep 26, 2008 at 17:32
1

Off the top of my head:

  1. They enforce tight-coupling. If your singleton resides on a different assembly than its user, the using assembly cannot ever function without the assembly containing the singleton.
  2. They allow for circular dependencies, e.g., Assembly A can have a singleton with a dependency on Assembly B, and Assembly B can use Assembly A's singleton. All without breaking the compiler.
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.