Advertisement
Guest User

How to avoid global variables in Delphi

a guest
Feb 10th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.53 KB | None | 0 0
  1. unit Settings;
  2.  
  3. interface
  4.  
  5. type
  6.   ISettings = interface
  7.   ['{DF0CDD58-0CDB-4828-B1A0-5A3C4D8E02B9}']
  8.   // private
  9.     function GetSingleInstance: Boolean;
  10.     procedure SetSingleInstance(const AValue: Boolean);
  11.   // public
  12.     function AppDir: String;
  13.     property SingleInstance: Boolean read GetSingleInstance write SetSingleInstance;
  14.   end;
  15.  
  16. function GlobalSettings: ISettings;
  17.  
  18. implementation
  19.  
  20. uses
  21.   Windows, SysUtils, Registry;
  22.  
  23. type
  24.   TSettings = class(TInterfacedObject, ISettings)
  25.   strict private
  26.     FAppDir: String;
  27.     FSingleInstance: Boolean;
  28.   private
  29.     class var FSettings: ISettings;
  30.   strict protected
  31.     FModified: Boolean;
  32.     procedure LoadSettings; virtual;
  33.     procedure SaveSettings; virtual;
  34.     function RegistryPath: String; virtual;
  35.  
  36.     { ISettings }
  37.     function AppDir: String;
  38.     function GetSingleInstance: Boolean;
  39.     procedure SetSingleInstance(const AValue: Boolean);
  40.   public
  41.     constructor Create; virtual;
  42.     destructor Destroy; override;
  43.   end;
  44.  
  45. { TSettings }
  46.  
  47. constructor TSettings.Create;
  48. begin
  49.   inherited Create;
  50.   LoadSettings;
  51. end;
  52.  
  53. destructor TSettings.Destroy;
  54. begin
  55.   SaveSettings;
  56.   inherited Destroy;
  57. end;
  58.  
  59. // ___
  60.  
  61. function TSettings.RegistryPath: String;
  62. begin
  63.   Result := '\Software\MyCompany\MyProduct';
  64. end;
  65.  
  66. procedure TSettings.LoadSettings;
  67. var
  68.   Reg: TRegIniFile;
  69. begin
  70.   FAppDir := ExtractFilePath(ParamStr(0));
  71.  
  72.   Reg := TRegIniFile.Create(KEY_READ);
  73.   try
  74.     Reg.RootKey := HKEY_CURRENT_USER;
  75.     FSingleInstance := Reg.ReadBool(RegistryPath, 'SingleInstance', False);
  76.   finally
  77.     FreeAndNil(Reg);
  78.   end;
  79.   FModified := False;
  80. end;
  81.  
  82. procedure TSettings.SaveSettings;
  83. var
  84.   Reg: TRegIniFile;
  85. begin
  86.   if not FModified then
  87.     Exit;
  88.   FModified := False;
  89.  
  90.   Reg := TRegIniFile.Create;
  91.   try
  92.     Reg.RootKey := HKEY_CURRENT_USER;
  93.     Reg.WriteBool(RegistryPath, 'SingleInstance', FSingleInstance);
  94.   finally
  95.     FreeAndNil(Reg);
  96.   end;
  97. end;
  98.  
  99. // ___
  100.  
  101. function TSettings.AppDir: String;
  102. begin
  103.   Result := FAppDir;
  104. end;
  105.  
  106. function TSettings.GetSingleInstance: Boolean;
  107. begin
  108.   Result := FSingleInstance;
  109. end;
  110.  
  111. procedure TSettings.SetSingleInstance(const AValue: Boolean);
  112. begin
  113.   FSingleInstance := AValue;
  114.   FModified := True;
  115. end;
  116.  
  117. // ___________________________________
  118.  
  119. function GlobalSettings: ISettings;
  120. begin
  121.   if TSettings.FSettings = nil then
  122.     TSettings.FSettings := TSettings.Create;
  123.   Result := TSettings.FSettings;
  124. end;
  125.  
  126. initialization
  127. finalization
  128.   TSettings.FSettings := nil;
  129. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement