小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Delphi設計模式之單例模式(Singleton Pattern)

 aaie_ 2013-04-05

 Singleton Pattern模式的設計意圖是:保證一個類僅有一個實例,并提供一個訪問他的全局訪問點。 
有人已經實現(xiàn)了這個模式,詳見代碼如下: 

  1. interface
  2. uses classes,SysUtils;
  3.  
  4. type
  5.   TSingletonList = class(TList);
  6.  
  7.   TSingletonl = class
  8.   public
  9.     constructor Create;virtual;
  10.     destructor Destroy; override;
  11.   protected
  12.     function Lookup(PSingleton: TSingletonl): boolean;
  13.   end;
  14.  
  15.   TChildSingletonl = class(TSingletonl);
  16.  
  17. var
  18.   Glob_Singletonlist: TSingletonList; //使用整體變量
  19.  
  20. implementation
  21.  
  22. constructor TSingletonl.Create;
  23. begin
  24.   if Lookup(self) then
  25.     Abort
  26.   else
  27.     Glob_Singletonlist.Add(self);
  28. end;
  29.  
  30. destructor TSingletonl.Destroy;
  31. begin
  32.   if Lookup(self) then
  33.   begin
  34.     Glob_Singletonlist.Remove(self);
  35.     inherited Destroy;
  36.   end;
  37. end;
  38.  
  39. function TSingletonl.Lookup(PSingleton: TSingletonl): boolean;
  40. var
  41.   i: word;
  42.   plSingleton: TSingletonl;
  43. begin
  44.   result := false;
  45.   if (Glob_Singletonlist = nilor (Glob_Singletonlist.Count = 0then
  46.   begin
  47.     Glob_Singletonlist := TSingletonList.Create;
  48.     result := false;
  49.     Exit;
  50.   end;
  51.   for i := 0 to Glob_Singletonlist.Count - 1 do
  52.   begin
  53.     plSingleton := Glob_Singletonlist.Get(i);
  54.     if (plSingleton.ClassName = PSingleton.ClassName) then
  55.       result := true;
  56.   end;
  57. end

他的這種方法是建立一個全局列表,通過查看全局列表的方式來實現(xiàn)singleton模式,這種方法除了沒有廣泛性的問題外,就是他的子類無法實現(xiàn)Singleton模式。所以此方法具有一定的局限性。另外一種的方法的實現(xiàn): 
  1. unit Unit4;
  2.  
  3. interface
  4. uses
  5.   SysUtils, Classes;
  6.  
  7. type
  8.   TDSingleton = class(TObject)
  9.   private
  10.     FCount: integer;
  11.   protected
  12.     constructor CreateInstance;
  13.     class function AccessInstance(Request: integer): TDSingleton;
  14.   public
  15.     constructor Create;
  16.     destructor Destroy; override;
  17.     class function Instance: TDSingleton;
  18.     class procedure ReleaseInstance; 
  19.   end;
  20.  
  21. implementation
  22.  
  23. {$J+}
  24. { TDSingleton }
  25.  
  26. class function TDSingleton.AccessInstance(Request: integer): TDSingleton;
  27. const
  28.   FInstance: TDSingleton = nil;
  29. begin
  30.   case Request of
  31.     0: ;
  32.     1:
  33.       begin
  34.         if not Assigned(FInstance) then
  35.           FInstance := CreateInstance;
  36.       end;
  37.     2: FInstance := nil;
  38.     else
  39.       raise Exception.CreateFmt('Illegal request %d in AccessInstance!', [Request]);
  40.   end;
  41.   result := FInstance;
  42. end;
  43.  
  44. constructor TDSingleton.Create;
  45. begin
  46.   raise Exception.CreateFmt('Access class %s only through Instance!', [ClassName]);
  47. end;
  48.  
  49. constructor TDSingleton.CreateInstance;
  50. begin
  51.   inherited Create;
  52. end;
  53.  
  54. destructor TDSingleton.Destroy;
  55. begin
  56.   if AccessInstance(0) = self then AccessInstance(2);
  57.   inherited Destroy;
  58. end;
  59.  
  60. class function TDSingleton.Instance: TDSingleton;
  61. begin
  62.   result := AccessInstance(1);
  63. end;
  64.  
  65. class procedure TDSingleton.ReleaseInstance;
  66. begin
  67.   AccessInstance(0).Free;
  68. end;
  69.  
  70. End. 


這里使用了指示符{$J+} 
$J在Delphi中的解釋是: 
The $J directive controls whether typed constants can be modified or not. In the {$J+} state, typed constants can be modified, and are in essence initialized variables. In the {$J-} state, typed constants are truly constant, and any attempt to modify a typed constant causes the compiler to report an error. 
Writeable consts refers to the use of a typed const as a variable modifiable at runtime. 
【翻譯】$J指示符控制常量類型是否能夠被修改。在{$J+}狀態(tài),類型常量可以被修改,并且本質上是初始化為變量。在{$J-}狀態(tài),類型常量是真實的常量,并且任何試圖修改一個類型常量都會引起編譯器報告錯誤。可寫常量參考運行時類型常量當作變量可修改的。 
在劉藝《Delphi設計模式》這本書中,也引用了這兩個例子。并分別做了闡述,這兩個例子是從網絡上找到。并做了相應的改寫。 當總是感覺Delphi使用此模式有些蹩腳,不像C++那么的舒暢。這是一個好的想法,但至少在Delphi中不是那么的實用。在delphi中,完全可以使用Initialization初始化一個變量,配合Finalization來釋放他。

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多