迭代器模式就是常见的集合中的Itrerator。

迭代器模式定义:提供一种方法顺序访问一个集合对象中的各个元素,有不暴露其内部是如何表示的。

可以用来处理集合之间不同类型数据的管理 

代码如下

//定义迭代器  

public interface Iterator

    {
        object Next();
        bool HasNext();
    }

    //定义菜单接口
    public interface Menu
    {
        Iterator CreateIterator();
    }
    //定义菜单属性类
    public class MenuItem
    {
        string name;
        string description;
        bool isVegetarian;
        double price;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        public bool IsVegetarian
        {
            get { return isVegetarian; }
            set { isVegetarian = value; }
        }

        public double Price
        {
            get { return price; }
            set { price = value; }
        }

        public MenuItem(string name,
            string description,
            bool isVegetarian,
            double price)
        {
            this.name = name;
            this.description = description;
            this.isVegetarian = isVegetarian;
            this.price = price;
        }
    }
    //定义晚餐哈希表迭代器
    public class CafeMenuIterator : Iterator
    {
        Hashtable menuItems = new Hashtable();
        int position = 1;

        public CafeMenuIterator(Hashtable menuItems)
        {
            this.menuItems = menuItems;
        }

        public bool HasNext()
        {
            return menuItems.Count < position ? false : true;
        }

        public object Next()
        {
            MenuItem menu = (MenuItem)menuItems[position];
            position += 1;
            return menu;
        }
    }
    //定义晚餐类
    public class CafeMenu : Menu
    {
        Hashtable menuItems = new Hashtable();

        public CafeMenu()
        {
            AddItem("Veggie Burger and Air Fries",
                 "Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
                 true, 3.99, 1);
            AddItem("Soup of the Day",
                "A cup of the soup of the day, with a side salad",
                false, 3.69, 2);
            AddItem("Burrito",
                "A large burrito, with whole pinto beans, salsa, guacamole",
                true, 4.29, 3);
        }

        public void AddItem(string name, string description,
            bool isVegetarian, double price, int index)
        {
            MenuItem menuItem = new MenuItem(name, description, isVegetarian, price);
            menuItems.Add(index, menuItem);
        }

        public Iterator CreateIterator()
        {
            return new CafeMenuIterator(menuItems);
        }
    }
    //定义午餐数组迭代器
    public class DinnerMenuIterator : Iterator
    {
        MenuItem[] items;
        int position = 0;

        public DinnerMenuIterator(MenuItem[] items)
        {
            this.items = items;
        }

        public bool HasNext()
        {
            if (position >= items.Length || items[position] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public object Next()
        {
            MenuItem menuItem = items[position];
            position += 1;
            return menuItem;
        }

    }
    //定义午餐类
    public class DinnerMenu : Menu
    {
        MenuItem[] menuItems;
        static int MAX_ITEMS = 6;
        int numberOfItem = 0;

        public DinnerMenu()
        {
            menuItems = new MenuItem[MAX_ITEMS];

            AddItem("Vegetarian BLT",
                "(Fakin') Bacon with lettuce & tomato on whole wheat",
                true,
                2.99);
            AddItem("BLT",
                "Bacon with lettuce & tomato on whole wheat",
                false,
                2.99);
            AddItem("Soup of the day",
                "Soup of the day, with a side of potato salad",
                false,
                3.29);
            AddItem("Hotdog",
                "A hot dog with saurkraut, relish, onions, topped with cheese",
                false,
                3.05);
            AddItem("Steamed Veggies and Brown Rice",
                "Steamed vegetables over brown rice",
                true,
                3.99);
            AddItem("Pasta",
                "Spaghetti with Marina Sauce and a slice of sourdough bread",
                true,
                3.89);
        }
        //
        public void AddItem(string name, string description,
            bool isVegetarian, double price)
        {
            MenuItem menuItem = new MenuItem(name, description,
                isVegetarian, price);
            if (numberOfItem >= MAX_ITEMS)
            {
                Console.WriteLine("Sorry, menu is full! Can't add item to menu");
            }
            else
            {
                menuItems[numberOfItem] = menuItem;
                numberOfItem += 1;
            }
        }

        public Iterator CreateIterator()
        {
            return new DinnerMenuIterator(menuItems);
        }
    }
    //服务员类
    public class Waitress
    {
        Menu dinnerMenu;
        Menu cafeMenu;

        public Waitress(Menu dinnerMenu, Menu cafeMenu)
        {
            this.cafeMenu = cafeMenu;
            this.dinnerMenu = dinnerMenu;
        }

        public string PrintMenu()
        {
            StringBuilder sb = new StringBuilder();
            Iterator dinnerIterator = dinnerMenu.CreateIterator();
            Iterator cafeIterator = cafeMenu.CreateIterator();

            sb.Append("\nLUNCH\n");
            sb.Append(PrintMenu(dinnerIterator));
            sb.Append("\nDinner\n");
            sb.Append(PrintMenu(cafeIterator));

            return sb.ToString();
        }

        public string PrintMenu(Iterator iterator)
        {
            StringBuilder sb = new StringBuilder();
            while (iterator.HasNext())
            {
                MenuItem menuItem = (MenuItem)iterator.Next();

                sb.Append(menuItem.Name + ", ");
                sb.Append(menuItem.Price + " -- ");
                sb.Append(menuItem.Description + "\n");
            }

            return sb.ToString();
        }
    }
    //测试示例
    public class IteratorTest
    {
        public static void Main()
        {
            Menu dinner = new DinnerMenu();
            Menu cafe = new CafeMenu();

            Waitress wait = new Waitress(dinner, cafe);

            Console.WriteLine(wait.PrintMenu());
        }
    }

 

 

 

 

posted on 2011-01-27 10:50  jackdesk  阅读(125)  评论(0编辑  收藏  举报