有这么多高手参与讨论,我很高兴,谢谢大家.
送个自己写的单根模式吧:
unit Unit2;
interface
type
Tsingleton= class(tobject)
private
RefCount: integer;
public
class function newInstance: tobject;override;
procedure freeInstance;override;
end;
implementation
uses sysutils;
procedure Tsingleton.freeInstance;
begin
dec(RefCount);
if RefCount=0 then
inherited freeInstance;
end;
class function Tsingleton.newInstance: tobject;
{$J+}
const
Instance: Tsingleton= nil;
begin
if Instance= nil then
begin
Instance:= inherited newInstance as Tsingleton;
end;
result:= Instance;
Inc(Instance.RefCount);
end;
end.