Delphi设计模式之单例模式(Singleton Pattern)

  •    Singleton Pattern模式的设计意图是:保证一个类仅有一个实例,并提供一个访问他的全局访问点。
    有人已经实现了这个模式,详见代码如下:
    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

    他的这种方法是建立一个全局列表,通过查看全局列表的方式来实现singleton模式,这种方法除了没有广泛性的问题外,就是他的子类无法实现Singleton模式。所以此方法具有一定的局限性。另外一种的方法的实现:
    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+}状态,类型常量可以被修改,并且本质上是初始化为变量。在{$J-}状态,类型常量是真实的常量,并且任何试图修改一个类型常量都会引起编译器报告错误。可写常量参考运行时类型常量当作变量可修改的。
    在刘艺《Delphi设计模式》这本书中,也引用了这两个例子。并分别做了阐述,这两个例子是从网络上找到。并做了相应的改写。 当总是感觉Delphi使用此模式有些蹩脚,不像C++那么的舒畅。这是一个好的想法,但至少在Delphi中不是那么的实用。在delphi中,完全可以使用Initialization初始化一个变量,配合Finalization来释放他。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值