德尔福Singleton模式 [英] Delphi Singleton Pattern

查看:124
本文介绍了德尔福Singleton模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个社区很多时候都是讨论过的,但是我在Delphi中找不到一个简单的单例模式的实现。
我在C#中有一个例子:

  public sealed class Singleton {
// Private Constructor
Singleton(){}

//私有构造函数实例化的私有对象
static readonly Singleton instance = new Singleton();

//获取对象的公共静态属性
public static Singleton UniqueInstance {
get {return instance;}
}

我知道在Delphi中没有像这样优雅的解决方案,我看到很多关于无法正确隐藏Delphi中构造函数的讨论(使它私有),所以我们需要覆盖NewInstance和FreeInstrance方法。我认为这是我在 http://ibeblog.com/?p=65

  type 
TTestClass = class
private
class var FInstance:TTestClass;
public
类函数GetInstance:TTestClass;
类析构函数DestroyClass;
结束

{TTestClass}
类析构函数TTestClass.DestroyClass;
begin
如果分配(FInstance)然后
FInstance.Free;
结束

类函数TTestClass.GetInstance:TTestClass;
begin
如果没有分配(FInstance)然后
FInstance:= TTestClass.Create;
结果:= FInstance;
结束

您对Singleton模式的建议是什么?



谢谢。

解决方案

我想如果我想要一个没有任何构造方式的类似的东西,我可能会使用与单元实现部分中包含的实现对象的接口。



我将通过一个全局函数(在接口部分声明)暴露界面。要实现线程安全,我将使用关键部分(或等效的)或可能仔细实施的双重检查锁定,但认识到天真的实现只能由于x86内存模型的强大性质而起作用。



它看起来像这样:

  unit uSingleton; 

接口

使用
SyncObjs;

type
ISingleton = interface
procedure DoStuff;
结束

function Singleton:ISingleton;

实现

类型
TSingleton =类(TInterfacedObject,ISingleton)
private
procedure DoStuff;
结束

{TSingleton}

程序TSingleton.DoStuff;
begin
end;

var
锁定:TCriticalSection;
_Singleton:ISingleton;

function Singleton:ISingleton;
begin
Lock.Acquire;
尝试
如果没有分配(_Singleton)然后
_Singleton:= TSingleton.Create;
结果:= _Singleton;
最后
Lock.Release;
结束;
结束

初始化
锁定:= TCriticalSection.Create;

finalization
Lock.Free;

结束。


I know this is discussed many times everywhere i the community but I just can't find a nice and simple implementation of a Singleton Pattern in Delphi. I have an example in C#:

public sealed class Singleton {
  // Private Constructor
  Singleton( ) { }

  // Private object instantiated with private constructor
  static readonly Singleton instance = new Singleton( );

  // Public static property to get the object
  public static Singleton UniqueInstance {
    get { return instance;}
}

I know there is no solution as elegant as this in Delphi and I saw a lot of discussion about no being able to correctly hide the constructor in Delphi (make it private) so we would need to override the NewInstance and FreeInstrance methods. Something along those lines I believe is the implementation I found on http://ibeblog.com/?p=65:

type
TTestClass = class
private
  class var FInstance: TTestClass;
public                              
  class function GetInstance: TTestClass;
  class destructor DestroyClass;
end;

{ TTestClass }
class destructor TTestClass.DestroyClass;
begin
  if Assigned(FInstance) then
  FInstance.Free;
end;

class function TTestClass.GetInstance: TTestClass;
begin
  if not Assigned(FInstance) then
  FInstance := TTestClass.Create;
  Result := FInstance;
end;

What would be your suggestion regarding the Singleton Pattern? Can it be simple and elegant and thread safe?

Thank you.

解决方案

I think if I wanted an object-like thing that didn't have any means of being constructed I'd probably use an interface with the implementing object contained in the implementation section of a unit.

I'd expose the interface by a global function (declared in the interface section). The instance would be tidied up in a finalization section.

To get thread-safety I'd use either a critical section (or equivalent) or possibly carefully implemented double-checked locking but recognising that naive implementations only work due to the strong nature of the x86 memory model.

It would look something like this:

unit uSingleton;

interface

uses
  SyncObjs;

type
  ISingleton = interface
    procedure DoStuff;
  end;

function Singleton: ISingleton;

implementation

type
  TSingleton = class(TInterfacedObject, ISingleton)
  private
    procedure DoStuff;
  end;

{ TSingleton }

procedure TSingleton.DoStuff;
begin
end;

var
  Lock: TCriticalSection;
  _Singleton: ISingleton;

function Singleton: ISingleton;
begin
  Lock.Acquire;
  Try
    if not Assigned(_Singleton) then
      _Singleton := TSingleton.Create;
    Result := _Singleton;
  Finally
    Lock.Release;
  End;
end;

initialization
  Lock := TCriticalSection.Create;

finalization
  Lock.Free;

end.

这篇关于德尔福Singleton模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆