-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathMVCBr.Patterns.Mediator.pas
363 lines (318 loc) · 8.19 KB
/
MVCBr.Patterns.Mediator.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
unit MVCBr.Patterns.Mediator;
interface
Uses System.Classes, System.SysUtils,
System.RTTI,
System.Generics.Collections;
type
TMVCBrParticipant = class;
TMVCBrParticipantClass = class of TMVCBrParticipant;
TMVCBrMediator<T: Class> = class;
TMVCBrParticipant = class(TInterfacedObject)
private
FID: TValue;
FMediator: TObject;
procedure SetID(const Value: TValue);
public
constructor Create; virtual;
destructor Destroy; override;
function Mediator: TMVCBrMediator<TMVCBrParticipant>;
property ID: TValue read FID write SetID;
procedure Release;
procedure Receive(AMessage: TValue); virtual;
procedure SendAll(AMessage: TValue); virtual;
procedure Send(AId: TValue; AMessage: TValue); virtual;
procedure SendOthers(AMessage: TValue); virtual;
end;
IMVCBrMediator<T:Class> = interface
['{F4612E9C-45EC-4CD2-BEAE-81C03A2FCB3A}']
procedure Release;
function GetItems(idx: Integer): T;
procedure SetItems(idx: Integer; const Value: T);
function This: TObject;
function Add(aValue: TMVCBrParticipant): Integer; overload;
function Add: TMVCBrParticipant; overload;
function Count: Integer;
property Items[idx: Integer]: T read GetItems
write SetItems;
procedure Remove(idx: Integer);
function DelegateTo(AFunc: TFunc<TMVCBrParticipant>): IMVCBrMediator<T>;
Function SendAll(AMessage: TValue): Boolean;
Function SendOthers(AIDTo: TValue; AMessage: TValue): Boolean;
function Send(AIDTo: TValue; AMessage: TValue): Integer;
end;
TMVCBrMediator<T: Class> = class(TInterfacedObject, IMVCBrMediator<T>)
private
FDelegateTo: TFunc<TMVCBrParticipant>;
FClass: TMVCBrParticipantClass;
FInstance: TThreadList<T>;
protected
function Invoke: TThreadList<T>; virtual;
function GetItems(idx: Integer): T; virtual;
procedure SetItems(idx: Integer; const Value: T); virtual;
procedure InternalDelete(AItem: T); virtual;
public
constructor Create(AClass: TMVCBrParticipantClass);
class Function New(AClass: TMVCBrParticipantClass): IMVCBrMediator<T>;
destructor Destroy; override;
procedure ForEach(AFunc: TFunc<T, Boolean>);
procedure ForEachDown(AFunc: TFunc<T, Boolean>);
function This: TObject; virtual;
procedure Clear; virtual;
procedure Release; virtual;
function DelegateTo(AFunc: TFunc<TMVCBrParticipant>): IMVCBrMediator<T>;
function Add(aValue: TMVCBrParticipant): Integer; overload; virtual;
function Add: TMVCBrParticipant; overload; virtual;
function Count: Integer; virtual;
property Items[idx: Integer]: T read GetItems
write SetItems;
procedure Remove(idx: Integer); virtual;
Function SendAll(AMessage: TValue): Boolean; virtual;
function Send(AIDTo: TValue; AMessage: TValue): Integer; virtual;
Function SendOthers(AIDTo: TValue; AMessage: TValue): Boolean;
end;
implementation
uses System.RTTI.Helper;
{ TMVCBrMediator<T> }
function TMVCBrMediator<T>.Add(aValue: TMVCBrParticipant): Integer;
begin
aValue.FMediator := self;
Invoke.Add(aValue);
result := Count - 1;
end;
function TMVCBrMediator<T>.Add: TMVCBrParticipant;
begin
if assigned(FDelegateTo) then
result := FDelegateTo()
else
result := FClass.Create;
Add(result);
end;
procedure TMVCBrMediator<T>.Clear;
var
obj: T;
begin
if assigned(FInstance) then
with FInstance.LockList do
try
while Count > 0 do
begin
obj := Items[0];
TMVCBrParticipant(obj).Release;
freeAndNil(obj);
end;
finally
FInstance.UnlockList;
end;
end;
function TMVCBrMediator<T>.Count: Integer;
begin
with Invoke do
with LockList do
try
result := Count;
finally
FInstance.UnlockList;
end;
end;
constructor TMVCBrMediator<T>.Create(AClass: TMVCBrParticipantClass);
begin
inherited Create;
FClass := AClass;
if not assigned(FClass) then
FClass := TMVCBrParticipant;
end;
function TMVCBrMediator<T>.DelegateTo(AFunc: TFunc<TMVCBrParticipant>)
: IMVCBrMediator<T>;
begin
FDelegateTo := AFunc;
end;
destructor TMVCBrMediator<T>.Destroy;
begin
Clear;
if assigned(FInstance) then
FInstance.DisposeOf;
inherited;
end;
procedure TMVCBrMediator<T>.ForEach(AFunc: TFunc<T, Boolean>);
var
i: Integer;
begin
if assigned(FInstance) and assigned(AFunc) then
with FInstance.LockList do
try
for i := 0 to Count - 1 do
if AFunc(Items[i]) then
break;
finally
FInstance.UnlockList;
end;
end;
procedure TMVCBrMediator<T>.ForEachDown
(AFunc: TFunc<T, Boolean>);
var
i: Integer;
begin
if assigned(FInstance) and assigned(AFunc) then
with FInstance.LockList do
try
for i := Count - 1 downto 0 do
if AFunc(Items[i]) then
break;
finally
FInstance.UnlockList;
end;
end;
function TMVCBrMediator<T>.GetItems(idx: Integer): T;
begin
result := nil;
if assigned(FInstance) then
with FInstance.LockList do
try
result := Items[idx];
finally
FInstance.UnlockList;
end;
end;
procedure TMVCBrMediator<T>.InternalDelete(AItem: T);
var
i: Integer;
begin
if assigned(FInstance) then
with FInstance.LockList do
try
i := IndexOf(AItem);
if i >= 0 then
delete(i);
finally
FInstance.UnlockList;
end;
end;
function TMVCBrMediator<T>.Invoke: TThreadList<T>;
begin
if not assigned(FInstance) then
FInstance := TThreadList<T>.Create;
result := FInstance;
end;
class function TMVCBrMediator<T>.New(AClass: TMVCBrParticipantClass)
: IMVCBrMediator<T>;
begin
result := TMVCBrMediator<T>.Create(AClass);
end;
procedure TMVCBrMediator<T>.Release;
var
i: Integer;
begin
if assigned(FInstance) then
with FInstance.LockList do
try
for i := Count - 1 Downto 0 do
TMVCBrParticipant(Items[i]).Release;
finally
FInstance.UnlockList;
end;
Clear;
end;
procedure TMVCBrMediator<T>.Remove(idx: Integer);
var
obj: T;
begin
if assigned(FInstance) then
with FInstance.LockList do
try
obj := Items[idx];
TMVCBrParticipant(obj).Release;
obj.DisposeOf;
finally
FInstance.UnlockList;
end;
end;
function TMVCBrMediator<T>.Send(AIDTo, AMessage: TValue): Integer;
begin
ForEach(
function(item: T): Boolean
begin
with TMVCBrParticipant(Item) do
if AIDTo.Equals(ID) then
begin
Receive(AMessage);
result := true; // break;
end;
end);
end;
function TMVCBrMediator<T>.SendAll(AMessage: TValue): Boolean;
begin
ForEach(
function(item: T): Boolean
begin
TMVCBrParticipant(item).Receive(AMessage);
end);
end;
function TMVCBrMediator<T>.SendOthers(AIDTo, AMessage: TValue): Boolean;
begin
ForEach(
function(item: T): Boolean
begin
with TMVCBrParticipant(Item) do
if not ID.Equals(AIDTo) then
Receive(AMessage);
end);
end;
procedure TMVCBrMediator<T>.SetItems(idx: Integer;
const Value: T);
begin
if assigned(FInstance) then
with FInstance.LockList do
try
Items[idx] := Value;
finally
FInstance.UnlockList;
end;
end;
function TMVCBrMediator<T>.This: TObject;
begin
result := self;
end;
{ TMVCBrParticipant }
constructor TMVCBrParticipant.Create;
begin
inherited Create;
FID := TGuid.NewGuid.ToString;
end;
destructor TMVCBrParticipant.Destroy;
begin
if assigned(FMediator) then
Mediator.InternalDelete(self);
inherited;
end;
function TMVCBrParticipant.Mediator: TMVCBrMediator<TMVCBrParticipant>;
begin
result := TMVCBrMediator<TMVCBrParticipant>(FMediator);
end;
procedure TMVCBrParticipant.Release;
begin
end;
procedure TMVCBrParticipant.Receive(AMessage: TValue);
begin
end;
procedure TMVCBrParticipant.Send(AId, AMessage: TValue);
begin
if AId.Equals(ID) then
Receive(AMessage)
else
Mediator.Send(AId, AMessage);
end;
procedure TMVCBrParticipant.SendAll(AMessage: TValue);
begin
Receive(AMessage);
SendOthers(AMessage);
end;
procedure TMVCBrParticipant.SendOthers(AMessage: TValue);
begin
if assigned(FMediator) then
Mediator.SendOthers(ID, AMessage);
end;
procedure TMVCBrParticipant.SetID(const Value: TValue);
begin
FID := Value;
end;
end.