static void Main(string[] args) {// 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; Console.WriteLine("Iterating over collection:"); for (Item item = iterator.First(); !iterator.IsDone; item = iterator.Next()) { Console.WriteLine(item.Name); } Console.WriteLine("Press any key to exit..."); Console.Read(); }
static void Main(string[] args) { Aggregate aggregate = new ConcreteAggregate(); aggregate[0] = "Zero"; aggregate[1] = "One"; aggregate[2] = "Two"; Iterator iterator = aggregate.CreateIterator(); for (object i = iterator.First(); !iterator.IsDone(); i = iterator.Next()) { Console.WriteLine(i); } }
public void Start() { ConcreteAggregate aggr = new ConcreteAggregate(); aggr[0] = "Hello "; aggr[1] = "Iterator "; aggr[2] = "Design "; aggr[3] = "Pattern"; // Create Iterator and provide aggregate Iterator i = aggr.CreateIterator(); object item = i.First(); while (item != null) { Console.Write(item); item = i.Next(); } Console.WriteLine(); }