static void Main(string[] args) { ConcreteAggregate passengers = new ConcreteAggregate(); passengers[0] = "大鸟"; passengers[1] = "小菜"; passengers[2] = "行李"; passengers[3] = "老外"; passengers[4] = "公司内部员工"; passengers[5] = "小偷"; Iterator iPassenger = new ConcreteIterator(passengers); //object item = iPassenger.First(); while (!iPassenger.IsDone()) { Console.WriteLine("{0}请买票", iPassenger.CuurentItem()); iPassenger.Next(); } Iterator iPassengerDesc = new ConcreteIteratorDesc(passengers); while (!iPassengerDesc.IsDone()) { Console.WriteLine("{0}请买票", iPassengerDesc.CuurentItem()); iPassengerDesc.Next(); } }
public static void Main(string[] args) { var aggregate = new ConcreteAggregate { [0] = "大鸟", [1] = "小菜", [2] = "行李", [3] = "老外", [4] = "公交员工", [5] = "小偷" }; Console.WriteLine("ConcreteIterator:"); Iterator iterator = new ConcreteIterator(aggregate); do { Console.WriteLine($"{iterator.CurrentItem()} 请买车票"); iterator.Next(); } while (!iterator.IsDone()); Console.WriteLine("ConcreteIteratorDesc:"); Iterator iteratorDesc = new ConcreteIteratorDesc(aggregate); do { Console.WriteLine($"{iteratorDesc.CurrentItem()} 请买车票"); iteratorDesc.Next(); } while (!iteratorDesc.IsDone()); Console.ReadLine(); }
static void Main(string[] args) { ConcreteAggegate <string> aggegate = new ConcreteAggegate <string>(); aggegate[0] = "小明"; aggegate[1] = "小红"; aggegate[2] = "小丁"; aggegate[3] = "小白"; //顺序读取 Iterator iterator = new ConcreteIterator <string>(aggegate); Console.WriteLine("顺序读取"); while (iterator.MoveNext()) { Console.WriteLine(iterator.CuurentItem()); } iterator = new ConcreteIteratorDesc <string>(aggegate); //逆序读取 Console.WriteLine("逆序读取"); while (iterator.MoveNext()) { Console.WriteLine(iterator.CuurentItem()); } Console.ReadKey(); }
public void TestCase1() { var aggregate = new ConcreteAggregate(); aggregate[0] = "Apple"; aggregate[1] = "Orange"; aggregate[2] = "Strawberry"; var iterator = new ConcreteIterator(aggregate); object item = iterator.First(); while (iterator.IsDone() == false) { Console.WriteLine(item); item = iterator.Next(); } }
static void Main(string[] args) { ConcreteAggregate a = new ConcreteAggregate(); a[0] = "Item A"; a[1] = "Item B"; a[2] = "Item C"; a[3] = "Item D"; ConcreteIterator i = new ConcreteIterator(a); Console.WriteLine("Iterating over collection:"); object item = i.First(); while (item != null) { Console.WriteLine(item); item = i.Next(); } Console.ReadKey(); }