迭代器模式

定义

在不暴露集合对象的内部信息的情况下提供一种方法实现快速顺序访问集合中的各个元素。

迭代器模式为遍历聚集结构提供改变索引下标,实现元素的访问方式。

类图

示例代码

  1  public interface IIterator
  2     {
  3         void First();
  4         void Last();
  5         bool MoveNext();
  6         object Current { get; }
  7     }
  8     /// <summary>
  9     /// 
 10     /// </summary>
 11     public class SIterator : IIterator
 12     {
 13         private CustomerList list = null;
 14         private int position = -1;
 15         public SIterator(CustomerList list)
 16         {
 17             this.list = list;
 18         }
 19         public void First()
 20         {
 21             position = 0;
 22         }
 23 
 24         public void Last()
 25         {
 26             position = list.Count - 1;
 27         }
 28 
 29         public bool MoveNext()
 30         {
 31             if (++position < list.Count)
 32             {
 33                 return true;
 34             }
 35             else
 36             { return false; }
 37         }
 38 
 39         public object Current
 40         {
 41             get
 42             {
 43                 return list[position];
 44             }
 45         }
 46     }
 47     public class RIterator : IIterator
 48     {
 49         private CustomerList list = null;
 50         private int position = 0;
 51         public RIterator(CustomerList list)
 52         {
 53             this.list = list;
 54         }
 55         public void First()
 56         {
 57             position = list.Count - 1;
 58         }
 59 
 60         public void Last()
 61         {
 62             position = 0;
 63         }
 64 
 65         public bool MoveNext()
 66         {
 67             position--;
 68             return position > 0;
 69         }
 70 
 71         public object Current
 72         {
 73             get { return list[position]; }
 74         }
 75     }
 76     public class CustomerList
 77     {
 78         private ArrayList list = null;
 79         public void Add(object value)
 80         {
 81             list.Add(value);
 82         }
 83         public void Remove(object value)
 84         {
 85             list.Remove(value);
 86         }
 87         public int Count
 88         {
 89             get
 90             {
 91                 return list.Count;
 92             }
 93         }
 94         public void Clear()
 95         {
 96             list.Clear();
 97         }
 98         public object this[int index]
 99         {
100             get
101             {
102                 if (index < 0 || index > list.Count)
103                 {
104                     throw new Exception("");
105                 }
106                 return list[index];
107             }
108 
109         }
110         public virtual IIterator CreateIterator()
111         {
112             return new SIterator(this);
113         }
114 
115     }
116     /// <summary>
117     /// 
118     /// </summary>
119     public class RList : CustomerList
120     {
121         public IIterator CreateIterator()
122         {
123             return new RIterator(this);
124         }
125     }
View Code

总结

迭代是针对集合的一种快速访问方式,同时不去暴露对象的内部结构。在编程中我们经常会用到的是foreach的语法糖,其内部就是一个迭代的过程。C#通过是实现IEnumerable接口的GetEnumerator方法,从而实现迭代。因为C#的集合本身已经帮我实现了,所以在程序中我们更多的是直接使用,很少定义自己的迭代器,但是作为程序员还是要了解其内部实现与功效的。

Top
收藏
关注
评论
原文地址:https://www.cnblogs.com/Joy-et/p/4845150.html