• 看菜谱点餐,迭代设计模式(Iterator)


    生活在城市,平时少不了去餐馆吃饭。或是加班下班之后,难免吃上宵夜之后,才回家。

    在餐馆或是大排档坐下,老板一般都会在第一时间,给你上茶水以及菜谱(在此博文中,这是重点)。我们接过菜谱之后,都会把菜谱遍历一遍,不放过任一美味与自己喜欢吃的佳肴。

    遍历一遍,此就是一种开发的设计模式,叫迭代器设计模式。

    迭代器模式,它提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。

    我们接过菜谱,使用两眼从头至尾或是从后往前看。菜谱聚集结构提供了第一项,下一项,是否结束,当前是哪一项了,是不是自己需要点的菜式。因此我们提供一个迭代的抽象类: 

    Iterator
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for Iterator
    /// </summary>
    namespace Insus.NET
    {
        public abstract class Iterator
        {
            public abstract object FirstItem();
            public abstract object NextItem();
            public abstract object CurrentItem();
            public abstract bool isDone();
        }
    }


    还要提供一个聚集的抽象类:

    Aggregate
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for Aggregate
    /// </summary>
    namespace Insus.NET
    {
        public abstract class Aggregate
        {
            public abstract Iterator CreateIterator();
        }
    }


    具体的迭代类:

    ConcreteIterator
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ConcreteIterator
    /// </summary>
    namespace Insus.NET
    {
        public class ConcreteIterator : Iterator 
        {
            
            //宣告一个具体聚集,菜谱
            private ConcreteAggregate _Aggregate;
    
            private int currentItem = 0;
            
    
            // 构造函数,传入真正的聚集对象传入。
            public ConcreteIterator(ConcreteAggregate aggregate)
            {
                this._Aggregate = aggregate;
            }
    
            //获取第一个菜谱项目名称
            public override object FirstItem()
            {
                return _Aggregate[0];
            }
    
            //下一个菜谱项目名称
            public override object NextItem()
            {
                object obj = null;
                currentItem ++;
                if (currentItem < _Aggregate.Count)
                    obj = _Aggregate[currentItem];
    
                return obj;
            }
    
    
            //浏览至当前的菜谱项目名称
            public override object CurrentItem()
            {
                return _Aggregate[currentItem];
            }
    
            //是否浏览至菜谱结尾。如果是,返回bool真(true)值。
            public override bool isDone()
            {
                return currentItem >= _Aggregate.Count ? true : false;
            }
        }
    }


    具体的聚集类,如菜谱:

    ConcreteAggregate
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Summary description for ConcreteAggregate
    /// </summary>
    namespace Insus.NET
    {
        public class ConcreteAggregate : Aggregate
        {
            List<object> items = new List<object>();
    
            public override Iterator CreateIterator()
            {
                return new ConcreteIterator(this);
            }
    
            public int Count
            {
                get
                {
                    return items.Count;
                }
            }
    
            //宣告一个索引器
            public object this[int index]
            {
                get
                {
                    return items[index];
                }
                set
                {
                    items.Insert(index, value);
                }
            }
        }
    }

    站点运行看看:

    IteratorDemo.aspx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Insus.NET;
    
    public partial class IteratorDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ConcreteAggregate ca = new ConcreteAggregate();
            ca[0] = "砂锅粥";
            ca[1] = "炸子鸡";
            ca[2] = "海棠冬菇";
            ca[3] = "佛手排骨";
            ca[4] = "广州文昌鸡";
            ca[5] = "潮式肠粉";
            ca[6] = "蚝皇凤爪";
    
            Iterator i = new ConcreteIterator(ca);
    
            object item = i.FirstItem();
    
            while (!i.isDone())
            {
                HttpContext.Current.Response.Write(i.CurrentItem());
                i.NextItem();
            }
        }
    }

     迭代设计模式源代码(.net 4.0):

     http://download.cnblogs.com/insus/CSharpNET/Insus_Iterator.rar

  • 相关阅读:
    9-15
    9-5
    8-26
    8-24
    7-20
    7-17
    我离职后要干些什么
    6-18
    5-28
    5-20
  • 原文地址:https://www.cnblogs.com/insus/p/2941104.html
Copyright © 2020-2023  润新知