yet another singleton? by the epc (xpro.com.au) * slide 1 yet another singleton? unique global...

11
Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Yet another singleton? Unique global objects play a part in nearly every program we write. Whether it be objects in Delphi's VCL such as Application, Screen or Clipboard, or our own constructions for status logging, memory tracking or app-specific requirements. These have come to be generically known as Singletons. The seminal text, "Design Patterns" (aka GOF), describes the intent of the Singleton Pattern thus: "To ensure a class has only one instance, and a global point of access" A cursory search of Delphi resources will reveal several published implementations of the Singleton Pattern. Most strictly follow GOF's generic implementation. I find the more interesting versions stick with the intent of the Pattern, but take advantage of Delphi's features, to produce Singletons which are used like any other class. This session will examine such a beast, TXPSingleton, developed in glorious oblivion of all others...

Upload: esther-cole

Post on 13-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 1

Yet another singleton?Yet another singleton?

Unique global objects play a part in nearly every program we write. Whether it be objects in Delphi's VCL such as Application, Screen or Clipboard, or our own constructions for status logging, memory tracking or app-specific requirements.

These have come to be generically known as Singletons. The seminal text, "Design Patterns" (aka GOF), describes the intent of the Singleton Pattern thus: "To ensure a class has only one instance, and a global point of access"

A cursory search of Delphi resources will reveal several published implementations of the Singleton Pattern. Most strictly follow GOF's generic implementation. I find the more interesting versions stick with the intent of the Pattern, but take advantage of Delphi's features, to produce Singletons which are used like any other class.

This session will examine such a beast, TXPSingleton, developed in glorious oblivion of all others...

Page 2: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 2

What's Wrong With Good What's Wrong With Good Ol' Global Variables?Ol' Global Variables?

Global variables are fine in small one-person projects. As the project grows in size and/or personnel, ensuring the globals are created before being referenced, and not referenced after being destroyed, can become a maintenance headache.

Creating and destroying globals in corresponding initialization and finalization sections is not a fail-safe solution. If the globals are referenced in other initialization blocks, it is quite easy to create a situation (via uses clause shuffling) where the global will be invalid when referenced.

Accessing globals via a function which conditionally creates the global is safe. This is the essence of the "classic" Singleton implementation. Safely destroying the Singleton is still an issue - an issue which is not covered at all by GOF. John Vlissides, of GOF fame, does cover the topic in a later book, "Pattern Hatching".

Page 3: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 3

TObject CustomisationTObject Customisation

TObject = class ...

class function NewInstance: TObject; virtual;

class function InitInstance( Instance:Pointer): TObject; procedure AfterConstruction; virtual;

procedure BeforeDestruction; virtual;

procedure FreeInstance; virtual; ...

end;

Allocate memory for instance. Called indirectly as first action of outermost constructor

Initialise memory and various structures. Must be called, implicitly or explicitly, by NewInstance.

User hook. Empty implementation in TObject. Called indirectly in outermost constructor.

User hook. Empty implementation in TObject. Called indirectly in outermost destructor.

Release memory and finalise various structures. Called indirectly as last action of outermost destructor.

Page 4: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 4

Object Creation and Destruction Object Creation and Destruction ScenariosScenarios

constructor TMyClass.Create; begin System._ClassCreate asm // If this is the "outermost" // constructor then... // Call to NewInstance entry // in VMT end inherited Create; ... // If this is the "outermost" // constructor then... System._AfterConstruction asm // Call to AfterConstruction entry // in VMT end end;

destructor TMyClass.Destroy; begin // If this is the "outermost" // destructor then... System._BeforeDestruction; asm // Call to BeforeDestruction entry // in VMT end .... inherited Destroy; // If this is the "outermost" // destructor then... System._ClassDestroy; asm // Call to FreeInstance entry // in VMT end end;

Page 5: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 5

TXPSingleton FeaturesTXPSingleton Features

Suitable as a base class for new or exisiting classes

Implementations based on TObject, TForm and TInterfacedObject.

Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness.

Supports creation of member data in subclasses with minimal additional coding.

Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Page 6: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 6

TXPSingleton FeaturesTXPSingleton Features

Suitable as a base class for new or exisiting classes

Implementations based on TObject, TForm and TInterfacedObject.

Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness.

Supports creation of member data in subclasses with minimal additional coding.

Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Page 7: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 7

TXPSingleton FeaturesTXPSingleton Features

Suitable as a base class for new or exisiting classes

Implementations based on TObject, TForm and TInterfacedObject.

Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness.

Supports creation of member data in subclasses with minimal additional coding.

Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Page 8: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 8

TXPSingleton FeaturesTXPSingleton Features

Suitable as a base class for new or exisiting classes

Implementations based on TObject, TForm and TInterfacedObject.

Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness.

Supports creation of member data in subclasses with minimal additional coding.

Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Page 9: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 9

TXPSingleton FeaturesTXPSingleton Features

Suitable as a base class for new or exisiting classes

Implementations based on TObject, TForm and TInterfacedObject.

Create and destroy like any other object. This greatly simplifies refactoring and reduces exposure of your design. Reference counting ensures uniqueness.

Supports creation of member data in subclasses with minimal additional coding.

Thread-safe. Uses critical sections to serialise access to reference counting, construction and destruction.

Page 10: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 10

Let's Code...Let's Code...

Page 11: Yet another Singleton? By The EPC (xpro.com.au) * Slide 1 Yet another singleton? Unique global objects play a part in nearly every program we write. Whether

Yet another Singleton? By The EPC (xpro.com.au) * Slide 11

ResourcesResources

Contact

Paul Spain, email: [email protected]

Slides and source code

Web: http://xpro.com.au

Books

"Design Patterns, Elements of Reusable Object-Oriented Software" by Erich Gamma et al. Addison Wesley. ISBN 0201633612

"Pattern Hatching, Design Patterns Applied" by John Vlissides. Addison Wesley. ISBN 0201432935

Other Singleton implementations

http://community.borland.com/article/0,1410,22576,00.html and follow-up comments from several people

Delphi Magazine, Issues 41 (Jan 1999) and 44 (April 1999)