Singleton模式之Delphi实现

2008-04-09 04:28:05来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

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;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:虚拟键码表(windows)

下一篇:完成DELPHI的不可能功能:宏替换!(如何根据字符串来创建对象)