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.
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.

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, that is, objects that are easier to implement, change, test, and reuse.
The singleton design pattern solves problems like:
The singleton design pattern describes how to solve such problems:
The key idea in this pattern is to make the class itself responsible for controlling its instantiation.
The hidden constructor 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.

Common uses

An implementation of the singleton pattern must:
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. The following is a sample implementation written in Java.

public final class Singleton

C# implementation


public sealed class Singleton

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

public static class Singleton

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.

public final class Singleton

Dart implementation


class Singleton

PHP implementation


class Singleton

Java Implementation


public class Coin

Kotlin Implementation

Kotlin object keyword declares a singleton class

object Coin

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 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;
end.

Usage:

procedure TForm3.btnCreateInstanceClick;
var
i: integer;
begin
for i := 0 to 5 do
ShowMessage;
end;