【设计模式】迭代器模式
一,概述
定义:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴漏该对象的内部表示。
作用:客户端可以不必知道数据集合的结构,而通过迭代器的接口来操作数据,不仅保护了数据结构,同时可以自定义数据的访问方式。
其实C++中的STL模板类中大量用到了此种设计模式!
二,示例
问题:售票员清点上车人数,然后挨个人遍历,让其买票。
class Program //客户端 { static void Main(string[] args) { ConcreteAggregate a = new ConcreteAggregate();//聚集类 a[0] = "大鸟"; a[1] = "小菜"; a[2] = "行李"; a[3] = "老外"; a[4] = "公交内部员工"; a[5] = "小偷"; Iterator i = new ConcreteIterator(a);//迭代器 //Iterator i = new ConcreteIteratorDesc(a); object item = i.First(); while (!i.IsDone())//遍历聚集类 { Console.WriteLine("{0} 请买车票!", i.CurrentItem()); i.Next(); } Console.Read(); } } abstract class Aggregate//抽象聚集类 { public abstract Iterator CreateIterator(); } class ConcreteAggregate : Aggregate//具体聚集类 { private IList<object> items = new List<object>(); public override Iterator CreateIterator() { return new ConcreteIterator(this); } public int Count { get { return items.Count; } } public object this[int index] { get { return items[index]; } set { items.Insert(index, value); } } } abstract class Iterator//抽象迭代器类(方便实现各种迭代策略) { public abstract object First(); public abstract object Next(); public abstract bool IsDone(); public abstract object CurrentItem(); } class ConcreteIterator : Iterator//具体迭代器类(升序访问) { private ConcreteAggregate aggregate; private int current = 0; public ConcreteIterator(ConcreteAggregate aggregate) { this.aggregate = aggregate; } public override object First() { return aggregate[0]; } public override object Next() { object ret = null; current++; if (current < aggregate.Count) { ret = aggregate[current]; } return ret; } public override object CurrentItem() { return aggregate[current]; } public override bool IsDone() { return current >= aggregate.Count ? true : false; } } class ConcreteIteratorDesc : Iterator//具体迭代器类(降序访问) { private ConcreteAggregate aggregate; private int current = 0; public ConcreteIteratorDesc(ConcreteAggregate aggregate) { this.aggregate = aggregate; current = aggregate.Count - 1; } public override object First() { return aggregate[aggregate.Count - 1]; } public override object Next() { object ret = null; current--; if (current >= 0) { ret = aggregate[current]; } return ret; } public override object CurrentItem() { return aggregate[current]; } public override bool IsDone() { return current < 0 ? true : false; } }