Singleton模式之Delphi實現 (轉)

worldblog發表於2007-12-12
Singleton模式之Delphi實現 (轉)[@more@]

type TSingleton = class(T) public A : Integer; class function NewInstance: TObject; overr; 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;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991815/,如需轉載,請註明出處,否則將追究法律責任。

相關文章