迭代器模式用于顺序访问集合对象的元素,而不须要知道集合对象的底层表示。Java和.Net等语言已经将迭代器做为其内部语法元素,好比在C#中,集合对象只须要实现IEnumberable接口,而后就能够用foreach来遍历了。
迭代器模式提示咱们要从使用者的角度考虑如何设计接口,如何对外提供访问内部对象的方式。即使咱们组织的对象系统内部结构很复杂,但对于客户程序而言最简单的方式莫过于经过for /foreach循环依次遍历,至于遍历过程当中的次序、分类筛选等则由目标类型本身封装。设计模式
GOF对迭代器模式描述为:
Provide a way to access the elements of an aggregate objectsequentially without exposing its underlying representation.
— Design Patterns : Elements of Reusable Object-Oriented Softwareide
UML类图:
this
代码实现设计
//迭代器接口 public interface IIterator<T> { T Next(); bool HasNext(); } //具体迭代器 public class ConcreteIterator<T> : IIterator<T> { private ConcreteAggretate<T> Aggretate; //成员变量,关联关系 private int cursor = 0; public ConcreteIterator(ConcreteAggretate<T> agg) { this.Aggretate = agg; } public bool HasNext() { return !(cursor >= Aggretate.Size); } public T Next() { if (HasNext()) { return Aggretate.GetELement(cursor++); } else { return default(T); } } } //聚合接口 public interface IAggretate<T> { public void Add(T obj); public void Remove(T obj); public int Size { get; } public T GetELement(int index); public IIterator<T> GetIterator(); } //具体聚合 public class ConcreteAggretate<T> : IAggretate<T> { private List<T> list = new List<T>(); // public void Add(T obj) { list.Add(obj); } public void Remove(T obj) { list.Remove(obj); } public IIterator<T> GetIterator() { return new ConcreteIterator<T>(this); //在局部方法中new实例,属依赖关系 } public int Size { get { return list.Count; } } public T GetELement(int index) { return list[index]; } }
调用者代码:code
IAggretate<int> aggretate = new ConcreteAggretate<int>(); aggretate.Add(9); aggretate.Add(8); aggretate.Add(7); IIterator<int> iterator = aggretate.GetIterator(); while (iterator.HasNext()) { Console.WriteLine(iterator.Next()); }
以上即是经典的迭代器模式的实现,这种模式给聚合对象增长了一个建立其迭代器对象的方法,迭代器的抽象定义和具体迭代器类型都做为一个额外的对象存在。
实际上C#已内置了对迭代器模式的支持,只须要实现IEnumerable接口便可,再也不须要从0开始,少了不少代码量:对象
public class ConcreteAggretate<T> : IEnumerable<T> { private List<T> list = new List<T>(); public void Add(T obj) { list.Add(obj); } public void Remove(T obj) { list.Remove(obj); } public IEnumerator<T> GetEnumerator() { foreach (var item in list) { yield return item; } } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } }
使用foreach遍历IEnumerable接口blog
var aggretate = ConcreteAggretate<int>(); aggretate.Add(9); aggretate.Add(8); aggretate.Add(7); foreach (var item in aggretate) { Console.WriteLine(item); }
优势接口
缺点
迭代器模式经过将存储数据和遍历数据的职责分离,为封装集合地复杂性、隔离变化提供了极大的遍历,但这种方式也有其固有的缺点:每次
增长新的聚合类都须要对应增长新的迭代器类,类的个数成对增长,这在必定程度上增长了系统的复杂性。element
参考书籍:
王翔著 《设计模式——基于C#的工程化实现及扩展》get