Singleton pattern

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
A class diagram exemplifying the singleton pattern.

In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.

The term comes from the mathematical concept of a singleton.


The singleton design pattern is one of the twenty-three well-known "Gang of Four" design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software with the aim of making it easier to implement, change, test, and reuse objects.[1]

The singleton design pattern solves problems by allowing it to:[2]

  • Ensure that a class only has one instance
  • Easily access the sole instance of a class
  • Control its instantiation
  • Restrict the number of instances
  • Access a global variable

The singleton design pattern describes how to solve such problems:

  • Hide the constructors of the class.
  • Define a public static operation (getInstance()) that returns the sole instance of the class.

In essence, the singleton pattern forces it to be responsible for ensuring that it is only instantiated once. A hidden constructor—declared private or protected—ensures that the class can never be instantiated from outside the class. The public static operation can be accessed by using the class name and operation name, e.g., Singleton.getInstance().

Common uses[edit]

Logging is a classic example of a singleton.[4]


Critics consider the singleton to be an anti-pattern as it introduces global state into an application, often unnecessarily. This in turn can place restrictions on any abstraction that uses the singleton, for example by preventing concurrent use of multiple instances.[5][6][7][8] Furthermore, because it is often exposed as a globally-visible accessor, its presence complicates dependency analysis by introducing a potential dependency on the singleton in all code it is visible to, thus requiring analysis of implementation details to determine if a dependency actually exists.[9]

Singletons also violate the single-responsibility principle,[5] because not only are they responsible for the singleton's normal task, it must also ensure that only one is instantiated; note that this relies on a "classic" singleton definition where it is responsible for enforcing its own uniqueness through, for example, a static getInstance() method.

Another drawback is that singletons are difficult to test[dubious ] because they carry global state for the duration of the program[dubious ]. Specifically, because unit testing requires loosely coupled classes in order to isolate what's being tested.[5] Additionally, when a certain class interacts with a singleton, that class and the singleton become tightly coupled[dubious ], making it impossible to test the class on its own without also testing the singleton[dubious ].[9]


Implementations of the singleton pattern must:

  • Ensure that only one instance of the singleton class ever exists; and
  • Provide global access to that instance.

Typically, this is done by:

The instance is usually stored as a private static variable; the instance is created when the variable is initialized, at some point before the static method is first called.


Singleton implementation in Java:[10]

public class Coin {

    private static final int ADD_MORE_COIN = 10;
    private int coin;
    private static Coin instance = new Coin(); // eagerly loads the singleton

    private Coin() {
        // private to prevent anyone else from instantiating

    public static Coin getInstance() {
        return instance;

    public int getCoin() {
        return coin;

    public void addMoreCoin() {
        coin += ADD_MORE_COIN;

    public void deductCoin() {
public class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;

Lazy initialization[edit]

A singleton implementation may use lazy initialization, where the instance is created when the static method is first invoked. If the static method might be called from multiple threads simultaneously, measures may need to be taken to prevent race conditions that could result in the creation of multiple instances. The following is a thread-safe implementation, using lazy initialization with double-checked locking, written in Java. In order to avoid the synchronization overhead while keeping lazy initialization with thread safety, the preferred approach in Java is to use the initialization-on-demand holder idiom.[citation needed]

public class Singleton {

    private static volatile Singleton instance = null;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized(Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();

        return instance;


class Singleton:
    __instance = None

    def __new__(cls, *args):
        if cls.__instance is None:
            cls.__instance = object.__new__(cls, *args)
        return cls.__instance


As of C++11, static local variable initialization is thread-safe and occurs after the first invocation.[11] This is a safer alternative than a namespace or class-scoped static variable.

class Singleton {
  static Singleton& GetInstance() {
    // Allocate with `new` in case Singleton is not trivially destructible.
    static Singleton* singleton = new Singleton();
    return *singleton;

  Singleton() = default;

  // Delete copy/move so extra instances can't be created/moved.
  Singleton(const Singleton&) = delete;
  Singleton& operator=(const Singleton&) = delete;
  Singleton(Singleton&&) = delete;
  Singleton& operator=(Singleton&&) = delete;


Singleton implementation in C#:

public sealed class Singleton
    private static volatile Singleton? _instance;
    private static readonly object _lock = new object();

    private Singleton() { }

    public static Singleton Instance
            if (_instance != null)
                return _instance;

            lock (_lock)
                if (_instance == null)
                    _instance = new Singleton();

            return _instance;

In C# static classes can also be used to create singletons, where the class itself is the singleton.

public static class Singleton
    public static MyOtherClass Instance { get; } = new MyOtherClass();


Singletons can be a useful tool when developing with Unity thanks to how classes are instantiated. This method is preferred over constructor hiding, as it is possible to instantiate an object with a hidden constructor in Unity. In order to prevent an instance from being overwritten, a check must be performed to ensure the instance is null. If it is not null, the GameObject containing the offending script should be destroyed.

If other components are dependent on the singleton, the script execution order should be modified in order to ensure that the component defining the singleton is executed first.

class Singleton : MonoBehaviour
    public static Singleton Instance { get; private set; }

    private void Awake()
        if (Instance != null && Instance != this)
            Instance = this;

Note that it is also possible to implement a singleton by only removing the offending script, not the GameObject itself, by instead calling Destroy(this).


Singleton implementation in Dart:

class Singleton {
    static Singleton get instance => Singleton._();


Singleton implementation in PHP:

class Singleton
    private static $instance = null;

    private function __construct() {}

    public static function getInstance(): self
        if (null === self::$instance) {
            self::$instance = new self();

        return self::$instance;


Kotlin object keyword declares a singleton class[10][12]

object Coin {
    private var coin: Int = 0

    fun getCoin():Int {
        return coin

    fun addCoin() {
        coin += 10

    fun deductCoin() {


A thread-safe implementation of a singleton in Pascal:

unit SingletonPattern;


  TTest = class sealed
  strict private
    FCreationTime: TDateTime;
    constructor Create;
    property CreationTime: TDateTime read FCreationTime;

function GetInstance: TTest;


  , SyncObjs

  FCriticalSection: TCriticalSection;
  FInstance: TTest;

function GetInstance: TTest;
    if not Assigned(FInstance) then
      FInstance := TTest.Create;

    Result := FInstance;

constructor TTest.Create;
  inherited Create;
  FCreationTime := Now;

  FCriticalSection := TCriticalSection.Create;




procedure TForm3.btnCreateInstanceClick(Sender: TObject);
  i: integer;
  for i := 0 to 5 do


Singleton implementation in Go.

package main

type Singleton struct {}

var instance *Singleton

func GetInstance() *Singleton {
	if instance != nil {
		return instance
	instance = new(Singleton)
	return instance

See also[edit]


  1. ^ a b Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 127ff. ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link)
  2. ^ "The Singleton design pattern - Problem, Solution, and Applicability". Retrieved 2017-08-16.
  3. ^ Soni, Devin (31 July 2019). "What Is a Singleton?". BetterProgramming. Retrieved 28 August 2021.
  4. ^ Rainsberger, J.B. (1 July 2001). "Use your singletons wisely". IBM. Archived from the original on 24 February 2021. Retrieved 28 August 2021.
  5. ^ a b c Button, Brian (25 May 2004). "Why Singletons are Evil". Being Scott Densmore. Microsoft. Archived from the original on 15 July 2021. Retrieved 28 August 2021.
  6. ^ Steve Yegge. Singletons considered stupid, September 2004
  7. ^ Hevery, Miško, "Global State and Singletons", Clean Code Talks, 21 November 2008.
  8. ^ Contieri, Maximiliano (13 July 2020). "Singleton Pattern: The Root of All Evil". Hacker Noon. Retrieved 28 August 2021.
  9. ^ a b "Why Singletons Are Controversial". Google Code Archive. Archived from the original on 6 May 2021. Retrieved 28 August 2021.
  10. ^ a b "Are you an Android Developer and not using Singleton Class yet?". 16 April 2020.
  11. ^ "Storage class specifiers: Static local variables". Cppreference. Retrieved 28 August 2021.
  12. ^ "Object declarations". Retrieved 2020-05-19.

External links[edit]