From a beginning C++ user's point of view the only reason I would see a singleton class useful is similar to when I want to use a static variable. Do you guys know when its most appropriate to use a singleton class?
8 Answers
The only time you should use a singleton is in the rare case where it is VITAL that there only ever be one of these objects. This simply doesn't come up nearly as often as you might think.
Quite often it's more than sufficient to just make a single instance of the class in question, but not put in all the machinery required to enforce the uniqueness of the object.
Every time I've used a singleton (or found one in the codebase I work with) I've ended up removing it because I needed to change the behavior, and the singleton machinery got in the way. I've yet to see a case in our code (payment processing) where a singleton actually made sense. I've got more than a few cases where I've only got one instance of a particular class, but none where enforcing that through the singleton pattern was necessary or helpful.

- 11,888
- 3
- 47
- 79
When you really need it, you'll know.
That might never happen.
When you think you want it, you're probably wrong.

- 62,466
- 11
- 102
- 153
I can't think of any instance where using a singleton class is appropriate.
In Java, they're mostly for organization. In C++, however, you should use namespaces instead.

- 24,113
- 33
- 111
- 170
-
3@Mark What are you talking about? – Maxpm Jul 10 '11 at 02:57
-
what is the purpose of having several things doing the same thing? – Mark Jul 10 '11 at 03:06
-
4@Mark The singleton pattern is *not* a feature of C++. It is a design pattern that is possible to *implement* in C++. – Maxpm Jul 10 '11 at 03:10
-
What do you think of static variable is that a feature? – Mark Jul 10 '11 at 03:14
-
2@Mark Namespaces and classes with static members are not the same. – Maxpm Jul 10 '11 at 03:25
-
@Mark Take a look at Java if you want to see a real beast ;) – Max Beikirch Aug 06 '13 at 12:14
I used to create singletons with global static accessors: UserRegistry::getInstance()
. Now, in the Dependency Injection era, I just use a registry. Helps with unit testing, too.

- 45,516
- 10
- 73
- 79
Singleton classes are glorified global variables. Use only when absolutely necessary, i.e. when an object has only a single instance throughout the scope of your implementation.

- 295,962
- 43
- 465
- 541
-
Why not just always use static variable? When do I never need to use singleton class? – Mark Jul 10 '11 at 02:41
-
http://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern – Mitch Wheat Jul 10 '11 at 02:43
-
-
3Maybe even *improved* global variables since they're at least confined to the class context. – aib Jul 10 '11 at 02:49
There was a thread on Hacker News yesterday talking about when it's NOT a good idea to use a singleton. There were several ideas there about when it IS a good idea to use a singleton (global configuration data, session management in webserver apps) but for every justification for singletons there are also good arguments as to why that's not a good idea. http://news.ycombinator.com/item?id=2743894

- 890
- 6
- 7
I can think of several reasons in the field of game development that having a class implemented as a singleton is absolutely required:
Physics Engines, Sprite Handlers, Gravity Handlers (if separate from Physics Engines), Input Parsers, etc.

- 10,297
- 11
- 59
- 88
-
7None of those are _absolutely_ required to be implemented as singletons. – Dennis Zickefoose Jul 10 '11 at 03:59
everything solvable without singleton. but i found myself using it to better control initialization and destructions of an object like graphic context. or when using state machine to better control state transitions. also, in (rare) cases when there's a system resource that's limited to 1 unit (e.g. console), and in when multithreading to prevent condition race. (https://refactoring.guru/design-patterns/singleton/cpp/example)

- 11
- 1
- 2
- 2