16.迭代器模式(Iterator Pattern)

using System;

namespace ConsoleApplication9
{
    class Program
    {
        /// <summary>
        /// 迭代器模式提供了一种方法顺序访问一个聚合对象(理解为集合对象)中各个元素,
        /// 而又无需暴露该对象的内部表示,这样既可以做到不暴露集合的内部结构,
        /// 又可让外部代码透明地访问集合内部的数据。
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Iterator iterator; 
            IListCollection list = new ConcreteList(); 
            iterator = list.GetIterator(); 
            while (iterator.MoveNext()) 
            { 
                int i = (int)iterator.GetCurrent(); 
                Console.WriteLine(i.ToString()); 
                iterator.Next(); 
            }

            Console.Read();
        }

        // 抽象聚合类
        public interface IListCollection
        {
            Iterator GetIterator();
        }

        // 迭代器抽象类
        public interface Iterator
        {
            //是否存在下个对象
            bool MoveNext();
            //获取下标对象
            Object GetCurrent();
            //下标加1
            void Next();
            //下标清0
            void Reset();
        }

        // 具体聚合类
        public class ConcreteList : IListCollection
        {
            int[] collection;
            public ConcreteList()
            {
                collection = new int[] { 2, 4, 6, 8 };
            }
            public Iterator GetIterator()
            {
                return new ConcreteIterator(this);
            }
            public int Length
            {
                get { return collection.Length; }
            }
            public int GetElement(int index)
            {
                return collection[index];
            }
        }

        // 具体迭代器类
        public class ConcreteIterator : Iterator
        {
            // 迭代器要集合对象进行遍历操作,自然就需要引用集合对象        
            private ConcreteList _list;
            private int _index;

            public ConcreteIterator(ConcreteList list)
            {
                _list = list;
                _index = 0;
            }

            public bool MoveNext()
            {
                if (_index < _list.Length)
                {
                    return true;
                }
                return false;
            }

            public Object GetCurrent()
            {
                return _list.GetElement(_index);
            }

            public void Reset()
            {
                _index = 0;
            }

            public void Next()
            {
                if (_index < _list.Length)
                {
                    _index++;
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/lgxlsm/p/4647065.html