Design patterns Singleton Singleton Design pattern (in general)


Note: The singleton is a design pattern.
But it also considered an anti-pattern.

The use of a singleton should be considered carefully before use. There are usually better alternatives.

The main problem with a singleton is the same as the problem with global variables. They introduce external global mutable state. This means that functions that use a singleton are not solely dependent on the input parameters but also the state of the singleton. This means that testing can be severely compromised (difficult).

The issues with singletons can be mitigated by using them in conjunction with creation patterns; so that the initial creation of the singleton can be controlled.