码迷,mamicode.com
首页 > 其他好文 > 详细

《大话设计模式》学习笔记16:迭代器模式

时间:2015-05-17 21:46:49      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:

  技术分享

  技术分享

乘车买票示例:

1.Iterator:

    public abstract class Iterator
    {
        public abstract object First();
        public abstract object Next();
        public abstract bool IsDone();
        public abstract object CurrentItem();
    }

2.Aggregate:

    public abstract class Aggregate
    {
        public abstract Iterator CreateIterator();
    }

3.ConcreteIterator:

    public class ConcreteIterator:Iterator
    {
        private ConcreteAggregate concreteAggregate;
        private int current = 0;
        public ConcreteIterator(ConcreteAggregate concreteAggregate)
        {
            this.concreteAggregate = concreteAggregate;
        }
        public override object First()
        {
            return concreteAggregate[0];
        }

        public override object Next()
        {
            object ret = null;
            current++;
            if (current < concreteAggregate.Count)
            {
                ret = concreteAggregate[current];
            }
            return ret;
        }

        public override bool IsDone()
        {
            return current >= concreteAggregate.Count ? true : false;
        }

        public override object CurrentItem()
        {
            return concreteAggregate[current];
        }
    }

4.ConcreteAggregate:

    public class ConcreteAggregate:Aggregate
    {
        private IList<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); } }
    }

5.客户端代码:

    class Program
    {
        static void Main(string[] args)
        {
            ConcreteAggregate concreteAggregate = new ConcreteAggregate();
            concreteAggregate[0] = "乘客1";
            concreteAggregate[1] = "乘客2";
            concreteAggregate[2] = "乘客3";

            Iterator iterator=new ConcreteIterator(concreteAggregate);
            object item = iterator.First();
            while(!iterator.IsDone())
            {
                Console.WriteLine("{0} 请买车票!", iterator.CurrentItem());
                iterator.Next();
            }
        }
    }

 

.NET的迭代器实现:

  IEumerator支持对非泛型集合的简单迭代接口:

    public interface IEumerator
    {
        object Current { get; }
        bool MoveNext();
        void Reset();
    }

  IEnumerable公开枚举数,该枚举数支持在非泛型集合上进行简单迭代:

    public interface IEnumerable
    {
        IEnumerator GetEnumerator();
    }

.NET实现迭代代码:

    class Program
    {
        static void Main(string[] args)
        {
            IList<string> items = new List<string>();
            items.Add("乘客1");
            items.Add("乘客2");
            items.Add("乘客3");

            foreach(var item in items)
            {
                Console.WriteLine("{0} 请买车票!", item);
            }
        }
    }

上面代码中编译器所做的工作:

IEumerator<string> e=items.GetEnumerator();
    
    while(e.MoveNext)
    {

    Console.WriteLine("{0} 请买车票!", e.Current);
  }

 

《大话设计模式》学习笔记16:迭代器模式

标签:

原文地址:http://www.cnblogs.com/walden1024/p/4510471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!