-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
59 lines (47 loc) · 1.1 KB
/
Program.cs
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
Item item1 = new Item("Item 1");
Item item2 = new Item("Item 2");
Item item3 = new Item("Item 3");
Item item4 = new Item("Item 4");
IAggregate concreteAggregate = new ConcreteAggregate();
IIterator iterator = concreteAggregate.CreateIterator();
iterator.Add(item1);
iterator.Add(item2);
iterator.Add(item3);
iterator.Add(item4);
while (iterator.HasNext())
{
var item = iterator.Next();
Console.WriteLine(item);
}
record Item(String Name)
{
}
interface IIterator
{
bool HasNext();
Item Next();
void Add(Item item);
bool Remove();
}
class ListIterator : IIterator
{
private readonly List<Item> _items = new();
private int _index = 0;
public bool HasNext() => _index < _items.Count;
public Item Next()
{
Item currentItem = _items[_index];
_index ;
return currentItem;
}
public void Add(Item item) => _items.Add(item);
public bool Remove() => _items.Remove(_items[_index]);
}
interface IAggregate
{
IIterator CreateIterator();
}
class ConcreteAggregate : IAggregate
{
public IIterator CreateIterator() => new ListIterator();
}