Exemplo n.º 1
0
        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();
            }
        }
        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();
        }