-
Notifications
You must be signed in to change notification settings - Fork 1
/
GetIt.pas
62 lines (49 loc) · 1.24 KB
/
GetIt.pas
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
unit GetIt;
interface
uses
GetIt.Interf,
System.Generics.Collections;
type
TGetIt = class(TInterfacedObject, IGetIt)
private
FInstances: TDictionary<string, TInterfacedObject>;
public
constructor Create;
destructor Destroy; override;
class function New: IGetIt;
function registerInstance(const value: TInterfacedObject): TInterfacedObject;
function I(const value: TInterfacedObject): TInterfacedObject;
function removeInstance(const value: TInterfacedObject): IGetIt;
end;
implementation
uses
System.SysUtils;
{ TGetIt }
constructor TGetIt.Create;
begin
FInstances := TDictionary<string, TInterfacedObject>.Create;
end;
destructor TGetIt.Destroy;
begin
FreeAndNil(FInstances);
inherited;
end;
function TGetIt.I(const value: TInterfacedObject): TInterfacedObject;
begin
FInstances.TryGetValue(value.ClassName, Result);
end;
class function TGetIt.New: IGetIt;
begin
Result := Self.Create;
end;
function TGetIt.registerInstance(const value: TInterfacedObject): TInterfacedObject;
begin
FInstances.AddOrSetValue(value.ClassName, value);
Result := I(value);
end;
function TGetIt.removeInstance(const value: TInterfacedObject): IGetIt;
begin
Result := Self;
FInstances.Remove(value.ClassName);
end;
end.