VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之使用C# (.NET Core) 实现迭代器设计模式 ((2)

public Hashtable MenuItems { get; } = new Hashtable(); public CafeMenu() { AddItem("Veggie Burger and Air Fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99); AddItem("Soup of the day", "A cup of the soup of the day, with a side salad", false, 3.69); AddItem("Burrito", "A large burrito, with whole pinto beans, salsa, guacamole", true, 4.29); } public IEnumerator CreateIEnumerator() { return MenuItems.GetEnumerator(); } public void AddItem(string name, string description, bool vegetarian, double price) { var menuItem = new MenuItem(name, description, vegetarian, price); MenuItems.Add(menuItem.Name, menuItem); } } }
复制代码

菜单的迭代器:

复制代码
using System;
using System.Collections;
using IteratorPattern.Menus;

namespace IteratorPattern.MenuIterators
{
    public class DinerMenuIterator: IEnumerator
    {
        private readonly MenuItem[] _menuItems;
        private int _position = -1;

        public DinerMenuIterator(MenuItem[] menuItems)
        {
            _menuItems = menuItems;
        }

        public bool MoveNext()
        {
            _position++;
            if (_position >= _menuItems.Length || _menuItems[_position] == null)
            {
                return false;
            }
            return true;
        }

        public void Reset()
        {
            _position = -1;
        }

        public object Current => _menuItems[_position];
    }
}
复制代码
复制代码
using System.Collections;
using System.Collections.Generic;

namespace IteratorPattern.MenuIterators
{
    public class PancakeHouseMenuIterator : IEnumerator
    {
        private readonly ArrayList _menuItems;
        private int _position = -1;

        public PancakeHouseMenuIterator(ArrayList menuItems)
        {
            _menuItems = menuItems;
        }

        public bool MoveNext()
        {
            _position++;
            if (_position >= _menuItems.Count || _menuItems[_position] == null)
            {
                return false;
            }
            return true;
        }

        public void Reset()
        {
            _position = -1;
        }

        public object Current => _menuItems[_position];
    }
}
复制代码

服务员:

复制代码
using System;
using System.Collections;
using IteratorPattern.Abstractions;
using IteratorPattern.Menus;

namespace IteratorPattern.Waitresses
{
    public class Waitress
    {
        private readonly ArrayList _menus;

        public Waitress(ArrayList menus)
        {
            _menus = menus;
        }

        public void PrintMenu()
        {
            var menuIterator = _menus.GetEnumerator();
            while (menuIterator.MoveNext())
            {
                var menu = menuIterator.Current as IMenu;
                PrintMenu(menu?.CreateIEnumerator());
            }
        }

        private void PrintMenu(IEnumerator iterator)
        {
            while (iterator.MoveNext())
            {
                if (iterator.Current != null)
                {
                    MenuItem menuItem;
                    if (iterator.Current is MenuItem item)
                    {
                        menuItem = item;
                    }
                    else
                    {
                        menuItem = ((DictionaryEntry)iterator.Current).Value as MenuItem;
                    }
                    Console.Write($"{menuItem?.Name}, ");
                    Console.Write($"{menuItem?.Price} -- ");
                    Console.WriteLine($"{menuItem?.Description}");
                }
            }
            Console.WriteLine();
        }
    }
}
复制代码

测试:

复制代码
        static void MenuTestDriveUsingIEnumerator()
        {
            var pancakeHouseMenu = new PancakeHouseMenu();
            var dinerMenu = new DinerMenu();
            var cafeMenu = new CafeMenu();

            var waitress = new Waitress(new ArrayList(3)
            {
                pancakeHouseMenu, dinerMenu, cafeMenu
            });
            waitress.PrintMenu();
        }
复制代码

 

 

深入浅出设计模式的C#实现的代码: https://github.com/solenovex/Head-First-Design-Patterns-in-CSharp

这篇先到这, 本章涉及到组合模式, 下篇文章再写.


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号