迭代器模式提供了一种方法顺序访问一个聚合对象(理解为集合对象)中各个元素,而又无需暴露该对象的内部表示,这样既可以做到不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据。
既然,迭代器模式承担了遍历集合对象的职责,则该模式自然存在2个类,一个是聚合类,一个是迭代器类。在面向对象涉及原则中还有一条是针对接口编程,所以,在迭代器模式中,抽象了2个接口,一个是聚合接口,另一个是迭代器接口,这样迭代器模式中就四个角色了,具体的类图如下所示:
- 迭代器角色(Iterator):迭代器角色负责定义访问和遍历元素的接口
- 具体迭代器角色(Concrete Iteraror):具体迭代器角色实现了迭代器接口,并需要记录遍历中的当前位置。
- 聚合角色(Aggregate):聚合角色负责定义获得迭代器角色的接口
- 具体聚合角色(Concrete Aggregate):具体聚合角色实现聚合角色接口。
class Program
{
static void Main(string[] args)
{
ConcreteList list = new ConcreteList();
Iterator iterator = list.GetInterator();
while (iterator.MoveNext())
{
int i = (int)iterator.GetCurrent();
Console.WriteLine(i.ToString());
iterator.Next();
}
Console.ReadKey();
}
// 迭代器抽象类
public interface Iterator
{
bool MoveNext();
Object GetCurrent();
void Next();
}
// 具体聚合类
public class ConcreteList//ConcreteList类
{
int[] collection;
public ConcreteList()//初始化Cincretelist
{
collection = new int[] { 1, 2, 3, 4 };
}
public Iterator GetInterator()
{
return new ConcreteIterator(this);
}
public int GetLength()
{
return collection.Length;
}
public int GetElement(int index)
{
return collection[index];
}
}
//具体迭代器类
public class ConcreteIterator : Iterator
{
private ConcreteList _list;//ConcreteList类
private int _index;
public ConcreteIterator(ConcreteList list)
{
_list = list;
}
public bool MoveNext()
{
if (_index < _list.GetLength())
{
return true;
}
else
{ return false; }
}
public void Next()
{
if (_index < _list.GetLength())
{
_index++;
}
}
public Object GetCurrent()
{
return _list.GetElement(_index);
}
}
}
迭代器模式的适用场景
在下面的情况下可以考虑使用迭代器模式:
- 系统需要访问一个聚合对象的内容而无需暴露它的内部表示。
- 系统需要支持对聚合对象的多种遍历。
- 系统需要为不同的聚合结构提供一个统一的接口。
迭代器模式的优缺点
由于迭代器承担了遍历集合的职责,从而有以下的优点:
- 迭代器模式使得访问一个聚合对象的内容而无需暴露它的内部表示,即迭代抽象。
- 迭代器模式为遍历不同的集合结构提供了一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作
迭代器模式存在的缺陷:
- 迭代器模式在遍历的同时更改迭代器所在的集合结构会导致出现异常。所以使用foreach语句只能在对集合进行遍历,不能在遍历的同时更改集合中的元素。