-
Notifications
You must be signed in to change notification settings - Fork 77
/
Structural.Decorator.Pattern.pas
94 lines (75 loc) · 1.71 KB
/
Structural.Decorator.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
unit Structural.Decorator.Pattern;
interface
type
IComponent = interface
['{8021ECE2-0D60-4C96-99AA-C5A6C515DF52}']
function Operation(): String;
End;
TComponent = class (TInterfacedObject, IComponent)
public
function Operation(): String;
end;
TDecoratorA = class (TInterfacedObject, IComponent)
private
FComponent: IComponent;
public
function Operation(): String;
constructor Create(c: IComponent);
end;
TDecoratorB = class (TInterfacedObject, IComponent)
private
FComponent: IComponent;
public
addedState: String;
function Operation(): String;
function AddedBehaviour(): String;
constructor Create(c: IComponent);
end;
TClient = class
class procedure Display(s: String; c: IComponent);
end;
implementation
{ TComponent }
function TComponent.Operation: String;
begin
Result := 'I am walking ';
end;
{ TDecoratorA }
constructor TDecoratorA.Create(c: IComponent);
begin
inherited Create;
Self.FComponent := c;
end;
function TDecoratorA.Operation: String;
var
s: String;
begin
s := Self.FComponent.Operation;
s := s + 'and listening to Classic FM ';
Result := s;
end;
{ TDecoratorB }
function TDecoratorB.AddedBehaviour: String;
begin
Result := 'and I bouth a capuccino ';
end;
constructor TDecoratorB.Create(c: IComponent);
begin
inherited Create;
Self.FComponent := c;
Self.addedState := 'past the coffe shop ';
end;
function TDecoratorB.Operation: String;
var
s: String;
begin
s := Self.FComponent.Operation;
s := s + 'to school ';
Result := s;
end;
{ TClient }
class procedure TClient.Display(s: String; c: IComponent);
begin
WriteLn(s + c.Operation);
end;
end.