-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhello.helloworldfactory.pas
More file actions
84 lines (71 loc) · 1.92 KB
/
hello.helloworldfactory.pas
File metadata and controls
84 lines (71 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
unit Hello.HelloWorldFactory;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils,
Hello.IHelloWorld,
Hello.HelloWorldImplementation,
SyncObjs;
type
/// <summary>
/// Singleton factory for creating <c>IHelloWorld</c> instances.
/// </summary>
/// <remarks>
/// Thread-safe implementation using <c>TCriticalSection</c>.
/// </remarks>
THelloWorldFactory = class
private
class var FInstance: THelloWorldFactory;
class var FLock: TCriticalSection;
public
/// <summary>
/// Class constructor initializes thread synchronization primitives.
/// </summary>
class constructor Create;
/// <summary>
/// Class destructor frees singleton instance and synchronization primitives.
/// </summary>
class destructor Destroy;
/// <summary>
/// Retrieves the singleton instance of the factory.
/// </summary>
/// <returns>The singleton <c>THelloWorldFactory</c> instance.</returns>
/// <remarks>
/// Ensures thread-safe lazy initialization of the singleton instance.
/// </remarks>
class function GetInstance: THelloWorldFactory;
/// <summary>
/// Creates a new <c>IHelloWorld</c> instance.
/// </summary>
/// <returns>A new <c>IHelloWorld</c> implementation instance.</returns>
function CreateHelloWorld: IHelloWorld;
end;
implementation
class constructor THelloWorldFactory.Create;
begin
FLock := TCriticalSection.Create;
end;
class destructor THelloWorldFactory.Destroy;
begin
FreeAndNil(FInstance);
FreeAndNil(FLock);
end;
class function THelloWorldFactory.GetInstance: THelloWorldFactory;
begin
if not Assigned(FInstance) then
begin
FLock.Acquire;
try
if not Assigned(FInstance) then
FInstance := THelloWorldFactory.Create;
finally
FLock.Release;
end;
end;
Result := FInstance;
end;
function THelloWorldFactory.CreateHelloWorld: IHelloWorld;
begin
Result := THelloWorldImplementation.Create;
end;
end.