-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathBehavioral.Visitor.Pattern.pas
162 lines (127 loc) · 2.81 KB
/
Behavioral.Visitor.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
unit Behavioral.Visitor.Pattern;
interface
uses
Classes, SysUtils;
type
IVisitor = interface;
TEmployee = class
public
FName: string;
FIncome: Double;
FVacationDays: integer;
constructor Create(nm: string; incm: Double; vacdays: integer);
procedure Accept(visitor: IVisitor);
end;
IVisitor = interface
procedure Visit(element: TEmployee);
end;
TIncomeVisitor = class(TInterfacedObject, IVisitor)
public
procedure Visit(element: TEmployee);
end;
TVacationVisitor = class(TInterfacedObject, IVisitor)
public
procedure Visit(element: TEmployee);
end;
TEmployees = class
private
FEmployees: TList;
public
constructor Create;
destructor Destroy; override;
procedure Attach(employee: TEmployee);
procedure Detach(employee: TEmployee);
procedure Accept(visitor: IVisitor);
end;
TClerk = class(TEmployee)
public
constructor Create;
end;
TDirector = class(TEmployee)
public
constructor Create;
end;
TPresident = class(TEmployee)
public
constructor Create;
end;
implementation
uses System.Types;
{ TElement }
procedure TEmployee.Accept(visitor: IVisitor);
begin
visitor.Visit(Self);
end;
constructor TEmployee.Create(nm: string; incm: Double; vacdays: integer);
begin
FName := nm;
FIncome := incm;
FVacationDays := vacdays;
end;
{ TIncomeVisitor }
procedure TIncomeVisitor.Visit(element: TEmployee);
var
employee: TEmployee;
begin
employee := TEmployee(element);
employee.FIncome := employee.FIncome * 1.10;
WriteLn(Format('%s new income: %g',[employee.FName, employee.FIncome]));
end;
{ TVacationVisitor }
procedure TVacationVisitor.Visit(element: TEmployee);
var
employee: TEmployee;
begin
employee := TEmployee(element);
employee.FVacationDays := employee.FVacationDays + 3;
WriteLn(Format('%s new vacation days: %d',[employee.FName, employee.FVacationDays]));
end;
{ TEmployees }
procedure TEmployees.Accept(visitor: IVisitor);
var
i: integer;
begin
for i := 0 to FEmployees.Count - 1 do begin
TEmployee(FEmployees[i]).Accept(visitor);
WriteLn;
end;
end;
procedure TEmployees.Attach(employee: TEmployee);
begin
FEmployees.Add(employee);
end;
constructor TEmployees.Create;
begin
inherited;
FEmployees := TList.Create;
end;
destructor TEmployees.Destroy;
var
i: integer;
begin
for i := 0 to FEmployees.Count - 1 do begin
TEmployee(FEmployees[i]).Free;
end;
FEmployees.Free;
inherited;
end;
procedure TEmployees.Detach(employee: TEmployee);
begin
FEmployees.Remove(employee);
end;
{ TClerk }
constructor TClerk.Create;
begin
inherited Create('Hank', 25000.0, 14);
end;
{ TDirector }
constructor TDirector.Create;
begin
inherited Create('Elly', 35000.0, 16);
end;
{ TPresident }
constructor TPresident.Create;
begin
inherited Create('Dick', 45000.0, 21);
end;
end.