Singleton mode Delphi implementation

zhaozj2021-02-11  187

Type

Tsingleton = Class (TOBJECT)

public

A: Integer;

Class Function Newinstance: TOBJECT; OVERRIDE

Excedure FreeInstance; Override;

Class Function Refcount: Integer;

END;

IMPLEMENTATION

VAR

Tsingleton = NIL;

REF_COUNT: INTEGER = 0;

Procedure tingsingleton.freeinstance;

Begin

Dec (ref_count);

IF (Ref_count = 0) THEN

Begin

= NIL;

// Destroy Private Variables Here

Inherited freeInstance;

END;

END;

Class Function Tsingleton.newinstance: TOBJECT

Begin

IF (Not Assigned) THEN

Begin

INSTANCE: = inherited newinstance as tingsingleton;

// Initialize Private Variables here: Like this:

Tsingleton (Instance) .a: 3D 1;

END;

RESULT: = Instance;

INC (Ref_count);

END;

Class function tsingleton.refcount: integer;

Begin

Result: = Ref_count;

END;

转载请注明原文地址:https://www.9cbs.com/read-5784.html

New Post(0)