-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathIteratorExample1.cs
More file actions
160 lines (137 loc) · 3.81 KB
/
Copy pathIteratorExample1.cs
File metadata and controls
160 lines (137 loc) · 3.81 KB
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
//-------------------------------------------------------------------------------------
// IteratorExample1.cs
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
//This real-world code demonstrates the Iterator pattern which is used to iterate over a collection of items and skip a specific number of items each iteration.
namespace IteratorExample1
{
public class IteratorExample1 : MonoBehaviour
{
void Start()
{
// Build a collection
Collection collection = new Collection();
collection[0] = new Item("Item 0");
collection[1] = new Item("Item 1");
collection[2] = new Item("Item 2");
collection[3] = new Item("Item 3");
collection[4] = new Item("Item 4");
collection[5] = new Item("Item 5");
collection[6] = new Item("Item 6");
collection[7] = new Item("Item 7");
collection[8] = new Item("Item 8");
// Create iterator
Iterator iterator = collection.CreateIterator();
// Skip every other item
iterator.Step = 2;
Debug.Log("Iterating collection:");
for (Item item = iterator.First();
!iterator.IsDone; item = iterator.Next())
{
Debug.Log(item.Name);
}
}
}
/// <summary>
/// A collection item
/// </summary>
class Item
{
private string _name;
// Constructor
public Item(string name)
{
this._name = name;
}
// Gets name
public string Name
{
get { return _name; }
}
}
/// <summary>
/// The 'Aggregate' interface
/// </summary>
interface IAbstractCollection
{
Iterator CreateIterator();
}
/// <summary>
/// The 'ConcreteAggregate' class
/// </summary>
class Collection : IAbstractCollection
{
private ArrayList _items = new ArrayList();
public Iterator CreateIterator()
{
return new Iterator(this);
}
// Gets item count
public int Count
{
get { return _items.Count; }
}
// Indexer
public object this[int index]
{
get { return _items[index]; }
set { _items.Add(value); }
}
}
/// <summary>
/// The 'Iterator' interface
/// </summary>
interface IAbstractIterator
{
Item First();
Item Next();
bool IsDone { get; }
Item CurrentItem { get; }
}
/// <summary>
/// The 'ConcreteIterator' class
/// </summary>
class Iterator : IAbstractIterator
{
private Collection _collection;
private int _current = 0;
private int _step = 1;
// Constructor
public Iterator(Collection collection)
{
this._collection = collection;
}
// Gets first item
public Item First()
{
_current = 0;
return _collection[_current] as Item;
}
// Gets next item
public Item Next()
{
_current += _step;
if (!IsDone)
return _collection[_current] as Item;
else
return null;
}
// Gets or sets stepsize
public int Step
{
get { return _step; }
set { _step = value; }
}
// Gets current iterator item
public Item CurrentItem
{
get { return _collection[_current] as Item; }
}
// Gets whether iteration is complete
public bool IsDone
{
get { return _current >= _collection.Count; }
}
}
}