111

Possible Duplicate:
What is so bad about Singletons?

Singleton Design Pattern: Pitfalls

Singleton anti-pattern

I've heard recently that Singleton is an anti-pattern. I know it has to do with the fact making a class singleton is like making that unique instance a global variable, but it's also doing a lot more than that (limiting the number of instances of that object, managing instantiation, etc..).

Why exactly is Singleton considered an anti-pattern? And what are the alternatives?

6
  • 2
    I know Singleton is overused - I've seen people use it in instances where it should not have been used - but it was the first time I heard it referred as an anti-pattern. Since then, I've been googling a little bit and found out quite some people consider it to be an anti-pattern, but I haven't found any acceptable explanation... Dour High Arch: Just google "singleton anti pattern" and you'll find hundreds of articles about it... But I can't find any that really justifies it. Which is why I am asking here. Oct 6, 2012 at 0:13
  • 1
    Dour High Arch: No duplicate. I did find that post. But it doesn't answer: "Why exactly is Singleton considered an anti-pattern? And what are the alternatives?" PS: GG deleting your previous comment. Oct 6, 2012 at 0:15
  • If you look at Dour's link, in the comments there is another link to a blog post which does answer the first part of the question pretty well. Also, as for alternatives, the other SO post discusses DI (Dependency Injection). If that doesn't answer your two questions, could you elaborate what you're looking for? Oct 6, 2012 at 0:25
  • See my answer here: michaelsafyan.com/tech/design/patterns/singleton Aug 13, 2015 at 4:49

4 Answers 4

103

To help with answering, here is more about the anti-pattern comment:

it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application

From: http://en.wikipedia.org/wiki/Singleton_pattern

For more on this you can look at: https://www.michaelsafyan.com/tech/design/patterns/singleton

Here is a great ending to the blog above:

In short, the singleton pattern makes code more complex, less useful, and a real pain to re-use or test. Eliminating singletons can be tricky, but it’s a worthwhile endeavour.

OK, so, the reason it is an anti-pattern is described well in this paragraph, and, as the author expresses, it tightly couples your code to the singleton.

If you find that you want to use a singleton, you may want to consider your design, but there are times where it is useful.

For example, once I had to write an application that could have at most one database connection, to process thousands of requests. So, a singleton makes sense since I am resource constrained to having only one instance.

But, generally this is used to simplify code, without thinking of the difficulties that will be introduced.

For example, and this applies to static classes also, if you unit test, or have concurrency, then the state of one request will change the state and that may cause problems, as the class calling the instance may be assuming the state is as it expected.

I think the best way to challenge the use is to think of how to handle it if your program is multi-threaded, and a simple way to do that is to unit test it, if you have several tests that run at one time.

If you find that you still need it, then use it, but realize the problems that will be encountered later.

13
  • 10
    Good answer. Just wanted to add though that even your DB example should be solved w/o a singleton. Easiest solution, w/o knowing the exact use case obviously, would be to have a factory which returns the single instance. It's a more elegant approach and doesn't violate SRP (as the Singleton pattern does). Also if the situation changes and your application needs to be modified to allow for multiple DB connections it will be a breeze to refactor compared to a singleton. There literally is no situation in which a singleton is preferable to another pattern.
    – Creynders
    Oct 8, 2012 at 7:29
  • 2
    @Creynders - Actually my problem was solved by having a separate program that ran, that just looked in a queue and processed what it found, so it had just one connection, but, a Singleton may have been a good fit there. I have had similar issues where I may have just 5 connections allowed, and something similar to the Singleton may work, as it would basically be a Factory that functions similar to a Singleton. Oct 8, 2012 at 12:41
  • The artical of Michael Safyan above is the best one I have found for singleton
    – camino
    Jul 13, 2017 at 17:47
  • 2
    The database is great example of antipattern - build whole program logic around current performance assumptions. Jan 3, 2018 at 12:32
  • Having a constraint to use one database is not a good reason to use singleton. This is like having a requirement to output one line of text, so you add code to insure that print is called only once. There is however a good use: You have a broken classes, than has hard-coded assumptions about a single resource (instead of excepting a db via constructor, and using a factory). The class will take time to fix, and for now you only need one instance. Therefore make it a singleton, not to indicate that we want only one instance, but to indicate that it is limited to one instance. Mar 2, 2018 at 23:21
35

I wouldn't exactly consider singleton to be an anti-pattern.

However, a singleton is basically a way to use global variables. And global variables are bad because any code anywhere in the system can change their values. So when debugging, it can be challenging to figure out which code path leads to the Singleton's current state.

7
  • 11
    False. Global variables can't be changed if they are hidden.
    – Andrew
    May 1, 2017 at 15:53
  • 1
    I never heard of "hidden" in terms of C++ standard, can you elaborate?
    – Joky
    Jul 10, 2017 at 19:46
  • @Joky I assume that hidden = abstracted. Mar 2, 2018 at 23:23
  • 2
    This is not the reason. Singleton does 3 things (violation of single responsibility principle): It does its main responsibility, it is a factory (see factory pattern), it insures a single instance (this is a bad idea, unless it is a temporary measure, to mark a class as in need of repeat, so that there can be more than one instance). Mar 2, 2018 at 23:26
  • What about singletons without sate, which should be the case for all singleton of course.
    – Tristan
    Sep 1, 2020 at 12:41
10

Singletons are often badly implemented. See double-checked-locking.

In which scope must an Singleton instance be unique. Like multi-Threaded environments, clustering etc.

Singletons can be hard to test.

Memory allocated to an Singleton can't be freed.

With excessiv usage of singletons you leave from objectoriented programming to procedural programming.

1
  • 18
    "Memory allocated to an Singleton can't be freed." Maybe that memory doesn't need to be freed, and that's why it was put in the Singleton? Plus, it can free the memory itself if it needs to.
    – Andrew
    May 1, 2017 at 15:55
10

I think it is considered an anti-pattern because a Singleton class can't be instantiated in a normal way by other object (except by calling such method commonly named "getInstance"). So, it will look like the class is being used directly without instantiating it to create a usable object first.

I agree with you that Singleton can serve as a global unique instance. I learned from some people that as alternatives for Singleton, we can use static and/or final variables, and we can also use enumeration (so that we can group multiple variables under one group name as what we usually do when we are working using a normal class/object).

However, these alternatives can only match the capability of a Singleton class in storing states/values. If we need unique functions to work with, those static/final variables and enumerations cannot help. In my opinion, this is the case when we need to use a Singleton class (when we need some unique functions to work with static/final states/values).

Cheers... :))

1
  • did you know that Java enums can have methods of varying visibility and even implement interfaces? I'm not advocating for the abusive overuse of enums, Singletons do have a limited purpose.
    – dan
    Apr 21 at 16:55

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