德尔福单例模式 [英] Delphi Singleton Pattern
问题描述
我知道社区到处都在讨论这个问题,但是我在 Delphi 中找不到一个很好且简单的单例模式实现.我在 C# 中有一个例子:
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; }
}
}
我知道在 Delphi 中没有像这样优雅的解决方案,我看到很多关于无法在 Delphi 中正确隐藏构造函数(将其设为私有)的讨论,因此我们需要覆盖 NewInstance 和 FreeInstance 方法.我相信这些方面的东西是我在 ibeblog.com - 上找到的实现Delphi:单例模式":
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 FreeInstance methods. Something along those lines I believe is the implementation I found on ibeblog.com - "Delphi: Singleton Patterns":
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?
谢谢.
推荐答案
我想如果我想要一个没有任何构造方法的类似对象的事物,我可能会使用与单元的实现部分中包含的实现对象的接口.
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.
为了获得线程安全性,我会使用临界区(或等效部分)或可能仔细实现的双重检查锁定,但认识到由于 x86 内存模型的强大特性,幼稚的实现只能工作.
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.
它看起来像这样:
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.
这篇关于德尔福单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!