Singleton pattern

From Wikipedia, the free encyclopedia
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.

Overview[]

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[]

  • The abstract factory, factory method, builder, and prototype patterns can use singletons.
  • 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:[3]
    • They do not pollute the global namespace (or, in languages with nested namespaces, their containing namespace) with unnecessary variables.[1]
    • They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources.

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

Criticism[]

Critics consider the singleton to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial because it often introduces unnecessary restrictions in situations where a singleton class would not be beneficial, thereby introducing global state into an application.[5][6][7][8]

Furthermore, because a singleton can be accessed anywhere by anything without having to use any parameters, any dependencies would not be immediately visible to developers. Consequently, developers would need to know "the inner workings of code to properly use it".[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 is 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 because they carry global state for the duration of the program. 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, making it impossible to test the class on its own without also testing the singleton.[9]

Implementations[]

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:

  • Declaring all constructors of the class to be private; and
  • Providing a static method that returns a reference to the instance.

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.

Java[]

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() {
        coin--;
    }
}
public final class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton getInstance() {
        return INSTANCE;
    }
}

Lazy initialization[]

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 final 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;
    }
}

Python[]

class Singleton:
    __instance = None

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

C++[]

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 {
 public:
  static Singleton& GetInstance() {
    // Allocate with `new` in case Singleton is not trivially destructible.
    static Singleton* singleton = new Singleton();
    return *singleton;
  }

 private:
  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;
};

C#[]

Singleton implementation in C#:

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

    private Singleton() { }
}

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

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

Unity[]

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) {
            Destroy(this.gameObject);
        } else {
            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).

Dart[]

Singleton implementation in Dart:

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

PHP[]

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[]

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() {
        coin--
    }
}

Pascal[]

A thread-safe implementation of a singleton in Pascal:

unit SingletonPattern;

interface

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

function GetInstance: TTest;

implementation

uses
  SysUtils
  , SyncObjs
  ;

var
  FCriticalSection: TCriticalSection;
  FInstance: TTest;

function GetInstance: TTest;
begin
  FCriticalSection.Acquire;
  try
    if not Assigned(FInstance) then
      FInstance := TTest.Create;

    Result := FInstance;
  finally
    FCriticalSection.Release;
  end;
end;

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

initialization
  FCriticalSection := TCriticalSection.Create;

finalization
  FreeAndNil(FCriticalSection);

end.

Usage:

procedure TForm3.btnCreateInstanceClick(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to 5 do
    ShowMessage(DateTimeToStr(GetInstance.CreationTime));
end;

See also[]

References[]

  1. ^ Jump up to: 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.CS1 maint: multiple names: authors list (link)
  2. ^ "The Singleton design pattern - Problem, Solution, and Applicability". w3sDesign.com. 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. ^ Jump up to: 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. ^ Jump up to: a b "Why Singletons Are Controversial". Google Code Archive. Google. Archived from the original on 6 May 2021. Retrieved 28 August 2021.
  10. ^ Jump up to: a b "Are you an Android Developer and not using Singleton Class yet?".
  11. ^ "Storage class specifiers: Static local variables". Cppreference. Retrieved 28 August 2021.
  12. ^ "Object declarations". Retrieved 2020-05-19.

External links[]

Retrieved from ""