-
Notifications
You must be signed in to change notification settings - Fork 77
/
Behavioral.Iterator.Pattern.pas
83 lines (64 loc) · 1.48 KB
/
Behavioral.Iterator.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
unit Behavioral.Iterator.Pattern;
interface
uses
Classes;
type
TCustomObject = class(TObject)
public
FName: string;
constructor Create(name: string);
procedure WriteName;
end;
TCustomList = class;
TCustomListEnumerator = class
private
FIndex: Integer;
FCustomList: TCustomList;
public
constructor Create(ACustomList: TCustomList);
function MoveNext: Boolean;
function GetCurrent: TCustomObject;
property Current: TCustomObject read GetCurrent;
end;
TCustomList = class(TList)
public
procedure Add(ACustomObject: TCustomObject);
function GetEnumerator: TCustomListEnumerator;
end;
implementation
{ TCustomObject }
constructor TCustomObject.Create(name: string);
begin
FName := name;
end;
procedure TCustomObject.WriteName;
begin
WriteLn('List member ' + FName);
end;
{ TCustomList }
procedure TCustomList.Add(ACustomObject: TCustomObject);
begin
inherited Add(ACustomObject);
end;
function TCustomList.GetEnumerator: TCustomListEnumerator;
begin
Result := TCustomListEnumerator.Create(Self);
end;
{ TCustomListEnumerator }
constructor TCustomListEnumerator.Create(ACustomList: TCustomList);
begin
inherited Create;
FIndex := -1;
FCustomList := ACustomList;
end;
function TCustomListEnumerator.GetCurrent: TCustomObject;
begin
Result := FCustomList.List[FIndex];
end;
function TCustomListEnumerator.MoveNext: Boolean;
begin
Result := FIndex < FCustomList.Count - 1;
if Result then
Inc(FIndex);
end;
end.