zoukankan      html  css  js  c++  java
  • 设计模式——迭代器模式

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

    适用性

    • 访问一个聚合对象的内容而无需暴露它的内部标识。
    • 支持对聚合对象的多种遍历
    • 为遍历不同的聚合结构提供一个统一的接口(即,支持多态迭代)

    参与者

    Iterator

    ——迭代器定义访问和遍历元素的接口

    ConcreteIterator

    ——具体迭代器实现迭代接口

    ——対该聚合遍历的时候跟踪当前位置

    Aggregate

    ——聚合定义创建相应的迭代器对象的接口

    ConcreteAggregate

    ——具体聚合实现创建相应迭代器的接口,该操作返回ConcreteIterator的一个是适当的实例

    1 // 抽象聚合类
    2     public interface IListCollection
    3     {
    4         Iterator GetIterator();
    5     }
    1   // 迭代器抽象类
    2     public interface Iterator
    3     {
    4         bool MoveNext();
    5         Object GetCurrent();
    6         void Next();
    7         void Reset();
    8     }
     1 // 具体聚合类
     2     public class ConcreteList : IListCollection
     3     {
     4         int[] collection;
     5         public ConcreteList()
     6         {
     7             collection = new int[] { 2, 4, 6, 8 };
     8         }
     9 
    10         public Iterator GetIterator()
    11         {
    12             return new ConcreteIterator(this);
    13         }
    14 
    15         public int Length
    16         {
    17             get { return collection.Length; }
    18         }
    19 
    20         public int GetElement(int index)
    21         {
    22             return collection[index];
    23         }
    24     }
     1  // 具体迭代器类
     2     public class ConcreteIterator : Iterator
     3     {
     4         // 迭代器要集合对象进行遍历操作,自然就需要引用集合对象
     5         private ConcreteList _list;
     6         private int _index;
     7 
     8         public ConcreteIterator(ConcreteList list)
     9         {
    10             _list = list;
    11             _index = 0;
    12         }
    13 
    14 
    15         public bool MoveNext()
    16         {
    17             if (_index < _list.Length)
    18             {
    19                 return true;
    20             }
    21             return false;
    22         }
    23 
    24         public Object GetCurrent()
    25         {
    26             return _list.GetElement(_index);
    27         }
    28 
    29         public void Reset()
    30         {
    31             _index = 0;
    32         }
    33 
    34         public void Next()
    35         {
    36             if (_index < _list.Length)
    37             {
    38                 _index++;
    39             }
    40                 
    41         }
    42     }
     1   // 客户端
     2     class Program
     3     {
     4         static void Main(string[] args)
     5         {
     6             Iterator iterator;
     7             IListCollection list = new ConcreteList();
     8             iterator = list.GetIterator();
     9 
    10             while (iterator.MoveNext())
    11             {
    12                 int i = (int)iterator.GetCurrent();
    13                 Console.WriteLine(i.ToString());
    14                 iterator.Next();
    15             }
    16 
    17             Console.Read();
    18         }
    19     }

    .NET中迭代器模式的应用

    在.NET下,迭代器模式中的聚集接口和迭代器接口都已经存在了,其中IEnumerator接口扮演的就是迭代器角色,IEnumberable接口则扮演的就是抽象聚集的角色,只有一个GetEnumerator()方法,关于这两个接口的定义可以自行参考MSDN。在.NET 1.0中,.NET 类库中很多集合都已经实现了迭代器模式,大家可以用反编译工具Reflector来查看下mscorlib程序集下的System.Collections命名空间下的类,这里给出ArrayList的定义代码,具体实现代码可以自行用反编译工具查看,具体代码如下所示:

     1 public class ArrayList : IList, ICollection, IEnumerable, ICloneable
     2 {
     3     // Fields
     4     private const int _defaultCapacity = 4;
     5     private object[] _items;
     6     private int _size;
     7     [NonSerialized]
     8     private object _syncRoot;
     9     private int _version;
    10     private static readonly object[] emptyArray;
    11 
    12     public virtual IEnumerator GetEnumerator();
    13     public virtual IEnumerator GetEnumerator(int index, int count);
    14 
    15     // Properties
    16     public virtual int Capacity { get; set; }
    17     public virtual int Count { get; }
    18     ..............// 更多代码请自行用反编译工具Reflector查看
    19 }

    学习于   http://www.cnblogs.com/zhili/p/IteratorPattern.html

  • 相关阅读:
    P1726 上白泽慧音
    P1993 小k的农场
    P1983 车站分级
    P1525 关押罪犯【二分+二分图】
    P1268 树的重量【构造】
    P1113 杂务
    F.Three pahs on a tree
    P1522 牛的旅行
    两个约束下的dp问题
    dp 最大正方形
  • 原文地址:https://www.cnblogs.com/cwmizlp/p/9149469.html
Copyright © 2011-2022 走看看