Class ASingleton

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type ASingleton = class(AReferenceCountedObject)

Description

This class represents a singleton, which is a class that can only have one instance throughout the lifetime of a program. The class makes use of class variables to maintain a reference to the first (and only) instance of the class, as well as a reference count to ensure that the instance is freed when the reference count reaches zero.

The class is designed so that code can simply attempt to construct a new instance of the class (via ASingleton.new or another mechanism). If an instance has already been constructed, that same instance is returned to the caller; otherwise, a new instance is created. When the caller is finished with the instance, it can be "freed" (via TObject.free). Only if the reference count of the instance has reached zero (0), will the instance actually be freed.

In neither case does the caller need to handle construction and destruction of the instance in any way that differs from the way other class instances are created and destroyed.

Hierarchy

Overview

Fields

Protected classvar MyInstance: ASingleton;

Methods

Public class constructor create;
Public class destructor destroy;
Public class function NewInstance: TObject; override;
Public function init: boolean; override;
Public procedure freeInstance; override;
Public class function Instance: TObject; virtual;

Description

Fields

Protected classvar MyInstance: ASingleton;

Refers to the first and only instance of the class

Methods

Public class constructor create;

Class constructor.

This method is defined to ensure that ASingleton.Instance is Nil before an attempt is made to construct an instance of the class. This step is necessary because FreePascal's runtime does not initialize the memory occupied by a class, so random junk data could potentially exist in the location used by the pointer, which would confuse our routine.

This method is called automatically by the runtime when the program first runs.

Public class destructor destroy;

Class destructor.

This method is defined to ensure that, in the event of programmer error which fails to release the final reference to the class instance, the instance is still freed – preventing memory leaks.

This method is called automatically by the runtime just prior to the end of the program.

Public class function NewInstance: TObject; override;

Allocate memory for a new instance of the class.

This method overrides the behavior inherited from TObject.NewInstance: it first checks to see whether Self.Instance is Nil; if not, then an instance has already been allocated and a reference to that instance is returned. If the instance has not yet been allocated, then the inherited routine is called.

The reference count of the instance is incremented by this method.

Public function init: boolean; override;

Initializer

Public procedure freeInstance; override;

Free the instance.

This method overrides the behavior inherited from TObject.freeInstance: it decrements Self.referenceCount and then checks to see whether the counter has reached zero (0). If so, it calls the inherited routine.

Overriding TObject.freeInstance should prevent the instance from being freed even if its destructor is called directly.

This method decrements the reference count of the instance.

Public class function Instance: TObject; virtual;

Retrieve a reference to the only instance of the singleton.

If an instance of the singleton has not yet been created, this method returns Nil. Otherwise, it returns a reference to the singleton.

The reference count of the instance is NOT changed by this method.


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11