Singleton模式之Delphi实现

类别:Delphi 点击:0 评论:0 推荐:

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;

本文地址:http://com.8s8s.com/it/it5532.htm