Singleton pattern

related topics
{math, number, function}
{system, computer, user}
{law, state, case}
{build, building, house}
{group, member, jewish}
{work, book, publish}
{style, bgcolor, rowspan}

In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects (say, five). Some consider it an anti-pattern, judging that it is overused, introduces unnecessary limitations in situations where a sole instance of a class is not actually required, and introduces global state into an application.[1][2][3][4][5][6]

In C++ it also serves to isolate from the unpredictability of the order of dynamic initialization, returning control to the programmer.


Common uses

  • The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation.
  • Facade objects are often Singletons because only one Facade object is required.
  • State objects [?] are often Singletons.
  • Singletons are often preferred to global variables because:
    • They don't pollute the global name space (or, in languages with namespaces, their containing namespace) with unnecessary variables.[7]
    • They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources.

Class diagram

Full article ▸

related documents
Fast Fourier transform
Closure (computer science)
Continued fraction
Axiom of choice
Tensor product
Algebraic geometry
Adjoint functors
Hilbert's tenth problem
Functional programming
Dedekind domain
Orthogonal matrix
Scheme (programming language)
Kolmogorov complexity
Design Patterns
Fourier series
Class (computer science)
Numeral system
Limit (category theory)
LR parser
Lie group
Elliptic curve cryptography
Binary search tree
Riemann integral
Recurrence relation
Μ-recursive function