在Delphi2010里面整了个单例模式,大家看看好用不?

ZuoBaoquan 2009-09-30 06:04:28
加精
[Code=Delphi(Pascal)]
(*
* This unit demonstrates how to implement the Singleton Pattern in Delphi 2010.
* The final reversion will be available in the Delphi Spring Framework.
*
* Zuo Baoquan
*
*)

unit SingletonPatternUnit;

interface

type
/// <summary>
/// Provides a simple, fast and thread-safe Singleton Pattern implementation.
/// </summary>
/// <description>
/// Singleton Pattern is defined as:
/// Ensure a class only has one instance, and provide a global point of access to it.
/// </description>
/// <remarks>
/// 1. Use Instance class property to get the singleton instance.
/// 2. Concrete Singleton Classes may override DoCreate/DoDestroy if necessary.
/// 3. Do not call Create/Free methods, otherwise an EInvalidOp exception will be raised.
/// </remarks>
/// <example>
/// <code>
/// TApplicationContext = class(TSingleton<TApplicationContext>)
/// protected
/// procedure DoCreate; override;
/// procedure DoDestroy; override;
/// end;
/// </code>
/// </example>
/// <author>Zuo Baoquan (Paul)</author>
TSingleton<T: class> = class //(TInterfaceBase)
strict private
class var fInstance: T;
class function GetInstance: T; static;
class destructor Destroy;
protected
procedure DoCreate; virtual;
procedure DoDestroy; virtual;
public
constructor Create;
destructor Destroy; override;
class property Instance: T read GetInstance;
end;

implementation

uses
Windows, SysUtils;

{$REGION 'TSingleton<T>'}

class destructor TSingleton<T>.Destroy;
begin
if fInstance <> nil then
begin
TSingleton<T>(fInstance).DoDestroy;
TSingleton<T>(fInstance).FreeInstance;
fInstance := nil;
end;
end;

constructor TSingleton<T>.Create;
begin
raise EInvalidOp.Create('Use Instance class property instead.');
end;

destructor TSingleton<T>.Destroy;
begin
if ExceptObject = nil then
raise EInvalidOp.Create('Free/Destroy.');
end;

class function TSingleton<T>.GetInstance: T;
var
obj: T;
begin
if fInstance = nil then
begin
obj := T(T.NewInstance);
TSingleton<T>(obj).DoCreate;
if InterlockedCompareExchangePointer(PPointer(@fInstance)^, PPointer(@obj)^, nil) <> nil then
begin
TSingleton<T>(obj).DoDestroy;
TSingleton<T>(obj).FreeInstance;
end;
end;
Result := fInstance;
end;

procedure TSingleton<T>.DoCreate;
begin
end;

procedure TSingleton<T>.DoDestroy;
begin
end;

{$ENDREGION}

end.

[/Code]
...全文
2863 90 打赏 收藏 转发到动态 举报
写回复
用AI写文章
90 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZuoBaoquan 2009-10-20
  • 打赏
  • 举报
回复
[Quote=引用 89 楼 flyfeifei66 的回复:]
一个单例搞这么复杂?
C#中


class MultiThread_Singleton
  {
      private static MultiThread_Singleton instance = null;
      private static object lockHelper = new object();
      private MultiThread_Singleton() { }
      public static MultiThread_Singleton Instance
      {
          get
          {
              if (instance == null)
              {
                  lock (lockHelper)
                  {
                      if (instance == null)
                      {
                          instance = new MultiThread_Singleton();
                      }
                  }
              }
              return instance;
          }
      }

  }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flyfeifei66/archive/2009/09/22/4581958.aspx
[/Quote]

根据我的了解,你需要把instance声明为volatile,才可以保证double-checked locking在多核处理器上的线程安全。

P.S. 我现在已经不用这种实现方法了,主要原因是不便于自动化测试,以及其他一些限制(比如无构造参数)
chentony 2009-10-15
  • 打赏
  • 举报
回复
学习
济南大飞哥 2009-10-15
  • 打赏
  • 举报
回复
一个单例搞这么复杂?
C#中


class MultiThread_Singleton
{
private static MultiThread_Singleton instance = null;
private static object lockHelper = new object();
private MultiThread_Singleton() { }
public static MultiThread_Singleton Instance
{
get
{
if (instance == null)
{
lock (lockHelper)
{
if (instance == null)
{
instance = new MultiThread_Singleton();
}
}
}
return instance;
}
}

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/flyfeifei66/archive/2009/09/22/4581958.aspx
karlwolf 2009-10-14
  • 打赏
  • 举报
回复
没想到还有那么多关心智慧女神!
jaffy 2009-10-09
  • 打赏
  • 举报
回复
10.1大家都不休息,还在讨论技术问题,佩服佩服
ZuoBaoquan 2009-10-09
  • 打赏
  • 举报
回复
[Quote=引用 79 楼 harryfin 的回复:]
D7之后Delphi陆续增加了很多语言特性,RTL也丰富了不少。像:abstract/sealed关键字,Extended Record, Generics, Anonymous Methods, Reflection, Unicode, class variable, class constructor/destructor, etc.

-------------------------------------------

这个abstract是指用在类上面的abstract吗?
[/Quote]
是的,只是标记而已,类还是可以直接创建的。标记为sealed的密封类,则不能被继承。
陌上花花 2009-10-09
  • 打赏
  • 举报
回复
学习下
VAEVA 2009-10-09
  • 打赏
  • 举报
回复
学习下
SUN00TAO 2009-10-09
  • 打赏
  • 举报
回复
支持 学习一下
chongxu1987 2009-10-09
  • 打赏
  • 举报
回复
hao
agel0ver 2009-10-08
  • 打赏
  • 举报
回复
学习
jia7007 2009-10-08
  • 打赏
  • 举报
回复
学习了!多谢!
Harryfin 2009-10-08
  • 打赏
  • 举报
回复
D7之后Delphi陆续增加了很多语言特性,RTL也丰富了不少。像:abstract/sealed关键字,Extended Record, Generics, Anonymous Methods, Reflection, Unicode, class variable, class constructor/destructor, etc.

-------------------------------------------

这个abstract是指用在类上面的abstract吗?
prabbit 2009-10-07
  • 打赏
  • 举报
回复
学习
haodff 2009-10-07
  • 打赏
  • 举报
回复
不错
lileixin934099968 2009-10-07
  • 打赏
  • 举报
回复
呵呵 知道了 以后跟住楼混
arbeng2002 2009-10-07
  • 打赏
  • 举报
回复
支持,学习,谢谢
sdgsfg 2009-10-07
  • 打赏
  • 举报
回复
学习学习
alk9000 2009-10-06
  • 打赏
  • 举报
回复
来支持你一下
chbondg2 2009-10-06
  • 打赏
  • 举报
回复
快点散分吧
加载更多回复(67)

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧