csingleton.pas
来自「Delphi深度探索,Delphi深度探索(第二版)」· PAS 代码 · 共 51 行
PAS
51 行
unit CSingleton;
interface
type
TSingleConfig=class(TObject)
private
FConfigPath: string;
procedure SetConfigPath(const Value: string);
public
class function GetInstance():TSingleConfig;
//系统配置路径
property ConfigPath:string read FConfigPath write SetConfigPath;
//...省略
class function NewInstance: TObject; override;
procedure FreeInstance;override;
end;
implementation
var
GlobalConfig:TSingleConfig=nil;//单元内私有的静态配置对象变量
{ TSingleConfig }
procedure TSingleConfig.FreeInstance;
begin
inherited;
GlobalConfig:=nil;
end;
class function TSingleConfig.GetInstance: TSingleConfig;
begin
if not Assigned(GlobalConfig) then
GlobalConfig:=TsingleConfig.Create();
Result:=GlobalConfig;
end;
class function TSingleConfig.NewInstance: TObject;
begin
if not Assigned(GlobalConfig) then
GlobalConfig:=TSingleConfig(inherited NewInstance);
Result:=GlobalConfig;
end;
procedure TSingleConfig.SetConfigPath(const Value: string);
begin
FConfigPath := Value;
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?