Walden1024

导航

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

  

  

乘车买票示例:

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);
  }

 

posted on 2015-05-17 21:05  Walden1024  阅读(181)  评论(0编辑  收藏  举报