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.

Class diagram exemplifying the singleton pattern.

Critics consider the singleton to be an anti-pattern in that it is frequently used in scenarios where it is not beneficial, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.[1][2][3]

Overview

The singleton[4] 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, that is, objects that are easier to implement, change, test, and reuse.

The singleton design pattern solves problems like:[5]

  • How can it be ensured that a class has only one instance?
  • How can the sole instance of a class be accessed easily?
  • How can a class control its instantiation?
  • How can the number of instances of a class be restricted?

The singleton design pattern describes how to solve such problems:

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

The key idea in this pattern is to make the class itself responsible for controlling its instantiation (that it is instantiated only once).
The hidden constructor (declared private) ensures that the class can never be instantiated from outside the class.
The public static operation can be accessed easily by using the class name and operation name (Singleton.getInstance()).

Common uses

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

Implementation

An implementation 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. The following is a sample implementation written in Java.

public final class Singleton {

    private static final Singleton INSTANCE = new Singleton();

    private Singleton() {}

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

C# implementation

public sealed class Singleton {

    private static readonly Singleton INSTANCE = new Singleton();

    private Singleton() {}

    public static Singleton Instance {
        get {
            return INSTANCE;
        }
    }
}

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

public static class Singleton {

    private static readonly MyOtherClass INSTANCE = new MyOtherClass();

    public static MyOtherClass Instance {
        get {
            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 of the class. The following is a thread-safe sample implementation, using lazy initialization with double-checked locking, written in Java.[lower-alpha 1]

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

Dart implementation

1 class Singleton {
2     
3     static Singleton _instance;
4     
5     static Singleton get instance => _instance ?? Singleton._();
6     
7     Singleton._() => _instance = this;
8 }

PHP implementation

 1 class Singleton {
 2     private static $instance = null;
 3 
 4     private function __construct(){}
 5 
 6     public static function getInstance(): self
 7     {
 8         if (null === self::$instance) {
 9             self::$instance = new self();
10         }
11 
12         return self::$instance;
13     }
14 }

Java Implementation [6]

 1 public class Coin {
 2 
 3     private static final int ADD_MORE_COIN = 10;
 4     private int coin;
 5     private static Coin instance = new Coin(); // Eagerly Loading of singleton instance
 6 
 7     private Coin(){
 8         // private to prevent anyone else from instantiating
 9     }
10 
11     public static Coin getInstance(){
12         return instance;
13     }
14 
15     public int getCoin(){
16         return coin;
17     }
18 
19     public void addMoreCoin(){
20         coin += ADD_MORE_COIN;
21     }
22 
23     public void deductCoin(){
24         coin--;
25     }
26 }

Kotlin Implementation [6]

Kotlin object keyword declares a singleton class[7]

 1 object Coin{
 2     private var coin: Int = 0
 3 
 4     fun getCoin():Int{
 5         return coin
 6     }
 7 
 8     fun addCoin(){
 9         coin += 10
10     }
11 
12     fun deductCoin(){
13         coin--
14     }
15 }

Delphi and Free Pascal implementation

GetInstance is thread safe implementation of Singleton.

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;

Notes

  1. In Java, to avoid the synchronization overhead while keeping lazy initialization with thread safety, the preferred approach is to use the initialization-on-demand holder idiom.

References

  1. Scott Densmore. Why singletons are evil, May 2004
  2. Steve Yegge. Singletons considered stupid, September 2004
  3. Clean Code Talks - Global State and Singletons
  4. 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)
  5. "The Singleton design pattern - Problem, Solution, and Applicability". w3sDesign.com. Retrieved 2017-08-16.
  6. "Are you an Android Developer and not using Singleton Class yet?".
  7. "Object declarations". Retrieved 2020-05-19.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.