-
Notifications
You must be signed in to change notification settings - Fork 77
/
Structural.Adapter.Pattern.pas
66 lines (52 loc) · 1.24 KB
/
Structural.Adapter.Pattern.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
63
64
65
66
unit Structural.Adapter.Pattern;
interface
uses Winapi.Windows, SysUtils;
type
TAdaptee = class
public
function SpecificRequest(a, b: Double): Double;
end;
ITarget = interface
['{7422D6B2-5601-4DB6-AF78-63524D1ED7A8}']
function Request(i: Integer): string;
end;
TAdapter = class(TAdaptee, ITarget, IInterface)
private
FRefCount: Integer;
public
function Request(i: Integer): string;
protected
function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
end;
implementation
{ TAdaptee }
function TAdaptee.SpecificRequest(a, b: Double): Double;
begin
Result := a / b;
end;
{ TAdapter }
function TAdapter.Request(i: Integer): string;
begin
Result := 'Rough estimate is '
+ IntToStr(Round(SpecificRequest(i, 3)));
end;
function TAdapter.QueryInterface(const IID: TGUID; out Obj): HResult;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TAdapter._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;
function TAdapter._Release: Integer;
begin
Result := InterlockedDecrement(FRefCount);
if Result = 0 then
Destroy;
end;
end.