您的位置:首页 > 编程语言 > Delphi

Singleton模式之Delphi实现

2002-09-19 09:23 716 查看
type
TSingleton = class(TObject)
public
A : Integer;
class function NewInstance: TObject; override;
procedure FreeInstance; override;
class function RefCount: Integer;
end;

implementation

var
Instance  : TSingleton  = nil;
Ref_Count : Integer     = 0;

procedure TSingleton.FreeInstance;
begin
Dec( Ref_Count );
if ( Ref_Count = 0 ) then
begin
Instance := nil;
// Destroy private variables here
inherited FreeInstance;
end;
end;

class function TSingleton.NewInstance: TObject;
begin
if ( not Assigned( Instance ) ) then
begin
Instance := inherited NewInstance as TSingleton;
// 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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: