Beispiel #1
0
 private void printMenu(Iterator iterator)
 {
     while (iterator.hasNext())
     {
         MenuItem menuItem = (MenuItem)iterator.next();
         Console.WriteLine(menuItem.getName() + ", ");
         Console.WriteLine(menuItem.getPrice() + " -- ");
         Console.WriteLine(menuItem.getDescription());
     }
 }
Beispiel #2
0
        static void Main(string[] args)
        {
            Aggregate a = new Aggregate();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            Iterator i = new Iterator(a);

            object item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }
        }
Beispiel #3
0
        private static void Main()
        {
            var collection = new Collection();

            for (int i = 0; i < 10; i++)
            {
                collection[i] = new Item($"Item {i}");
            }

            // create iterator
            var iterator = new Iterator(collection)
            {
                Step = 2
            };

            Console.WriteLine("Iterating over collection: ");

            for (Item item = iterator.First(); !iterator.IsDone; item = iterator.Next())
            {
                Console.WriteLine(item.Name);               
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            var a = new Aggregate<string>();

            a[0] = "Item A";
            a[1] = "Item B";
            a[2] = "Item C";
            a[3] = "Item D";

            var i = new Iterator<string>(a);

            var item = i.First();

            while (item != null)
            {
                Console.WriteLine(item);
                item = i.Next();
            }

            var infinite = Infinite.FromValue("hello world!");

            foreach (string str in infinite)
                Console.WriteLine(str);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            /*
             *
             * Aggregate 集合接口 对应 Iterator 迭代器接口
             * BookShelf 实现了Aggregate的具体集合 对应  BookShelfIterator实现了Iterator的具体迭代器
             * BookShelfIterator是知道BookShelf的具体实现的
             *
             * 使用时是面向接口变成 Aggregate 和 Iterator
             * Iterator it=bookShelf.iterator();
             * while (it.HasNext())
             *{
             *   Book book = (Book)it.Next();
             *   Console.WriteLine(book.GetName());
             *}
             */

            BookShelf bookShelf = new BookShelf(4);

            bookShelf.AppendBook(new Book("Around the World in 80 Days"));
            bookShelf.AppendBook(new Book("Bible"));
            bookShelf.AppendBook(new Book("Cinderella"));
            bookShelf.AppendBook(new Book("Daddy-Long-Legs"));

            Iterator it = bookShelf.iterator();

            while (it.HasNext())
            {
                Book book = (Book)it.Next();
                Console.WriteLine(book.GetName());
            }

            NewBookShelf newBookShelf = new NewBookShelf();

            newBookShelf.AppendBook(new Book("Around the World in 80 Days"));
            newBookShelf.AppendBook(new Book("Bible"));
            newBookShelf.AppendBook(new Book("Cinderella"));

            Iterator newIt = newBookShelf.iterator();

            //当BookShelf与BookShelfIterator代码发生变化时,便利的代码并没有发生变化
            while (newIt.HasNext())
            {
                Book book = (Book)newIt.Next();
                Console.WriteLine($"New {book.GetName()}");
            }

            //
            List <Book> list = new List <Book>();

            list.Add(new Book("Around the World in 80 Days"));
            list.Add(new Book("Bible"));
            list.Add(new Book("Cinderella"));

            foreach (var bk in list)
            {
                Console.WriteLine($"List {bk.GetName()}");
            }

            Console.ReadKey();
        }