Design Pattern Examples
Overview of object-oriented design patterns
IteratorContainer_Class.cs
Go to the documentation of this file.
1
6
7using System.Collections.Generic;
8
10{
14 public class ItemPair
15 {
16 public string Key;
17 public string Value;
18
24 public ItemPair(string key, string value)
25 {
26 Key = key;
27 Value = value;
28 }
29 }
30
31
51 public interface IIterator<TItemType>
52 {
56 void Reset();
57
63 TItemType? Next();
64 }
65
66
90 class Iterator<TItemType> : IIterator<TItemType>
91 {
95 TItemType[] _items;
96
101
106 public Iterator(TItemType[] items)
107 {
108 _items = (TItemType[])items.Clone();
109 }
110
116 public TItemType? Next()
117 {
118 TItemType? item = default(TItemType);
119
120 if (_index < _items.Length)
121 {
122 item = _items[_index];
123 ++_index;
124 }
125 return item;
126 }
127
131 public void Reset()
132 {
133 _index = 0;
134 }
135 }
136
137
143 {
144 // Hardcoded data to be iterated over.
145 // The number of keys must match the number of values.
146 // This container is not a dictionary despite the use of Keys and
147 // Values as names but it was the simplest form of data I could come
148 // up with that didn't use any of the C# containers.
149 string[] _keys = new string[] { "One", "Two", "Three" };
150 string[] _values = new string[] { "Value 1", "Value 2", "Value 3" };
151
158 {
159 List<ItemPair> items = new List<ItemPair>();
160
161 int numItems = _keys.Length;
162 for (int index = 0; index < numItems; ++index)
163 {
164 items.Add(new ItemPair(_keys[index], _values[index]));
165 }
166
167 // Note: the ToArray() creates a copy of the list but as an
168 // array.
169 return new Iterator<ItemPair>(items.ToArray());
170 }
171
178 {
179 return new Iterator<string>(_keys);
180 }
181
188 {
189 return new Iterator<string>(_values);
190 }
191 }
192}
Represents a key/value pair where the key and value are strings.
ItemPair(string key, string value)
Constructor.
Represents a container that offers up two kinds of iterators for the hardcoded contents.
IIterator< string > GetValues()
Retrieve an iterator over the "value" part of the data, where the data returned from the iterator is ...
IIterator< string > GetKeys()
Retrieve an iterator over the "key" part of the data, where the data returned from the iterator is a ...
IIterator< ItemPair > GetItems()
Retrieve an iterator over the data that returns an ItemPair object containing both key and value for ...
Represents an iterator for a container by implementing the IIterator interface.
int _index
The index into the _items array to the next item.
void Reset()
Reset the iterator to the beginning.
Iterator(TItemType[] items)
Constructor.
TItemType[] _items
The array of items to iterate over.
TItemType? Next()
Returns the next item in the iteration or null, if there are no more items.
Represents an iterator for some type. This is a forward-only iterator in that it can only start at 0 ...
void Reset()
Start iteration from beginning of container.
TItemType? Next()
Retrieve the next item from the container.
The namespace containing all Design Pattern Examples implemented in C#.