zoukankan      html  css  js  c++  java
  • 设计模式学习笔记——迭代器模式(Iterator)

    1.特点:将对集合的访问与遍历从集合对象中分离出来到迭代器中。

    2.概念:迭代器模式(Iterator)就是分离了聚合对象的遍历行为,抽象出一个迭代器来负责这样既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部数据。

    3.类图:

    4.程序实现

    // 抽象聚合类
        public interface IListCollection
        {
            Iterator GetIterator();
        }
     
        
    // 迭代器抽象类
        public interface Iterator
        {
            bool MoveNext();
            Object GetCurrent();
            void Next();
            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++;
                }
     
            }
        }
     
        
    // 客户端
        class Program
        {
            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();
            }
        }
    

      

  • 相关阅读:
    scrapy 模块功能流程--转
    CP三次握手和四次分手--转
    获取免费IP--代码--转
    爬虫介绍+Jupyter Notebook--转
    In Ubuntu, How to install Chinese Pinyin with Fcitx?
    对json文件进行简单读写操作
    ubuntu 中wget (下载)命令用法
    如何更改Ubuntu 16.04 默认Python版本方法
    如何将本地文件上传通过命令行命令上传到远程服务器上
    Ubuntu上,如何成功的安装pygrib
  • 原文地址:https://www.cnblogs.com/ice-baili/p/4710404.html
Copyright © 2011-2022 走看看