HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

Singleton: How should it be used

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howusedsingletonshould

Problem

Edit:
From another question I provided an answer that has links to a lot of questions/answers about singletons: More info about singletons here:

So I have read the thread Singletons: good design or a crutch?

And the argument still rages.

I see Singletons as a Design Pattern (good and bad).

The problem with Singleton is not the Pattern but rather the users (sorry everybody). Everybody and their father thinks they can implement one correctly (and from the many interviews I have done, most people can't). Also because everybody thinks they can implement a correct Singleton they abuse the Pattern and use it in situations that are not appropriate (replacing global variables with Singletons!).

So the main questions that need to be answered are:

  • When should you use a Singleton



  • How do you implement a Singleton correctly



My hope for this article is that we can collect together in a single place (rather than having to google and search multiple sites) an authoritative source of when (and then how) to use a Singleton correctly. Also appropriate would be a list of Anti-Usages and common bad implementations explaining why they fail to work and for good implementations their weaknesses.

So get the ball rolling:

I will hold my hand up and say this is what I use but probably has problems.

I like "Scott Myers" handling of the subject in his books "Effective C++"


Good Situations to use Singletons (not many):



  • Logging frameworks



  • Thread recycling pools




```
/*
* C++ Singleton
* Limitation: Single Threaded Design
* See: http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
* For problems associated with locking in multi threaded applications
*
* Limitation:
* If you use this Singleton (A) within a destructor of another Singleton (B)
* This Singleton (A) must be fully constructed before the constructor of (B)
* is called.
*/
class MySingleton
{
private:
// Private Constructor
MySingleton();
// Stop th

Solution

Answer:

Use a Singleton if:

  • You need to have one and only one object of a type in system



Do not use a Singleton if:

  • You want to save memory



  • You want to try something new



  • You want to show off how much you know



  • Because everyone else is doing it (See cargo cult programmer in wikipedia)



  • In user interface widgets



  • It is supposed to be a cache



  • In strings



  • In Sessions



  • I can go all day long



How to create the best singleton:

  • The smaller, the better. I am a minimalist



  • Make sure it is thread safe



  • Make sure it is never null



  • Make sure it is created only once



  • Lazy or system initialization? Up to your requirements



  • Sometimes the OS or the JVM creates singletons for you (e.g. in Java every class definition is a singleton)



  • Provide a destructor or somehow figure out how to dispose resources



  • Use little memory

Context

Stack Overflow Q#86582, score: 201

Revisions (0)

No revisions yet.