Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.
Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
Singleton should be considered only if all three of the following criteria are satisfied:
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.
The Singleton pattern can be extended to support access to an application-specific number of instances.
The “static member function accessor” approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.
Deleting a Singleton class/instance is a non-trivial design problem. See “To Kill A Singleton” by John Vlissides for a discussion.
Make the class of the single instance responsible for access and “initialization on first use”. The single instance is a private static attribute. The accessor function is a public static method.
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, “The President of the United States” is a global point of access that identifies the person in the office.
static
attribute in the “single instance” class. static
accessor function in the class. protected
or private
. Delphi Sample
UML(Class Diagram)
unit Pattern; interface uses SysUtils; type TSingle = class (Tobject) public procedure WriteCount; class function NewInstance: TObject; override; procedure FreeInstance; override; end; implementation var Instance: TObject = nil; nCount: Integer = 0; procedure TSingle.FreeInstance; begin Dec (nCount); if nCount = 0 then begin inherited FreeInstance; Instance := nil; end; end; class function TSingle.NewInstance: TObject; begin if not Assigned (Instance) then Instance := inherited NewInstance; Result := Instance; Inc (nCount); end; procedure TSingle.WriteCount; begin WriteLn('Count = ' + IntToStr(nCount)); end; end.
------------------------------------------------------------------------------------------------------------------------------
program Creational.Singleton.Pattern; {$APPTYPE CONSOLE} uses SysUtils, Pattern in 'Pattern.pas'; var s1, s2: TSingle; begin // ReportMemoryLeaksOnShutdown := DebugHook 0; try s1 := TSingle.Create; s2 := TSingle.Create; try s1.WriteCount; s2.WriteCount; ReadLn; finally s1.Free; s2.Free; end; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.