static void Main(string[] args) { IAggregate aggregate = new ConcreateAggregate("Item1", "Item2", "Item3", "Item4"); IIterator iterator = aggregate.CreateIterator(); if (iterator.IsDone()) { iterator.First(); } while (!iterator.IsDone()) { Console.WriteLine(iterator.CurrentItem()); iterator.Next(); } Console.ReadKey(); }
static void Main(string[] args) { //迭代器模式 ConcreateAggregate aggregate = new ConcreateAggregate(); aggregate[0] = "长腿美女"; aggregate[1] = "帅哥"; aggregate[2] = "小偷"; aggregate[3] = "警察"; aggregate[3] = "内部员工售票员"; //Iterator.Base.Iterator iterator = new ConcreteInerator(aggregate); Iterator.Base.Iterator iterator = new ConcreteIneratorDesc(aggregate); object item = iterator.First(); while (!iterator.IsDone()) { Console.WriteLine("{0},请您买票!", iterator.CurrentItem()); iterator.Next(); } Console.Read(); }
public ConcreateIterator(ConcreateAggregate integerList) { this.integerList = integerList; this.index = 0; }