Singleton模式之Delphi实现
[入库:2005年8月18日] [更新:2007年3月25日]
|
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;
| |
本文关键:Singleton 模式
本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)