9、设计模式中的单件模式,在Delphi中可以用什么方式创建。gdli,是不是这样?unit Singleton;interfacetype TSingletonObj = class(TObject) private class function GetInstance(GetOrSet:Boolean;
AInst:TObject):TObject;
public class function NewInstance: TObject;
override;
procedure FreeInstance;
override;
class function IncRefCount(IncOrDec:Boolean):Integer;
end;
implementationprocedure TSingletonObj.FreeInstance;
begin
if IncRefCount(False) = 0 then
begin
inherited FreeInstance;
GetInstance(False, nil);
end;
end;
class function TSingletonObj.GetInstance(GetOrSet: Boolean;
AInst: TObject): TObject;{$J+}const Inst: TObject = nil;{$J-}begin
if not GetOrSet then
Inst := AInst;
Result := Inst;
end;
class function TSingletonObj.IncRefCount(IncOrDec:Boolean): Integer;{$J+}const RefCount: Integer = 0;{$J-}begin
if IncOrDec then
Inc(RefCount) else
if RefCount > 0 then
Dec(RefCount);
Result := RefCount;
end;
class function TSingletonObj.NewInstance: TObject;
begin
Result := GetInstance(True, nil);
if Result = nil then
begin
Result := inherited NewInstance;
GetInstance(False, Result);
end;
IncRefCount(True);
end;
end.