面向物件設計中,集合物件內部結構多樣 (陣列、鏈表、樹等),但外部僅關心兩點:資料儲存結構與遍歷方式。單一職責原則要求分離遍歷行為,迭代器模式應運而生:抽象出迭代器類別負責遍歷,不暴露集合內部表示,讓外部代碼透明存取集合元素。
意圖:提供方法循序存取聚合物件元素,且不暴露內部表示。(GoF)
結構圖:

早期電視撥盤需手動掃描每個頻道;現代遙控器「上/下一台」按鈕即為迭代器:使用者只關心節目內容,不需知道頻道號碼:

public interface IList { IIterator GetIterator(); }
public interface IIterator {
bool MoveNext();
object CurrentItem();
void First();
void Next();
}
public class ConcreteList : IList {
private int[] _list = {1,2,3,4,5};
public IIterator GetIterator() => new ConcreteIterator(this);
public int Length => _list.Length;
public int GetElement(int index) => _list[index];
}
public class ConcreteIterator : IIterator {
private ConcreteList _list; private int _index;
public ConcreteIterator(ConcreteList list) { _list = list; _index = 0; }
public bool MoveNext() => _index < _list.Length;
public object CurrentItem() => _list.GetElement(_index);
public void First() => _index = 0;
public void Next() { if (_index < _list.Length) _index++; }
}
IList list = new ConcreteList();
IIterator it = list.GetIterator();
while (it.MoveNext()) {
Console.WriteLine(it.CurrentItem());
it.Next();
}
public interface IEnumerator {
object Current { get; }
bool MoveNext();
void Reset();
}
public interface IEnumerable { IEnumerator GetEnumerator(); }
public class Persons : IEnumerable {
private string[] _names;
public Persons(params string[] names) { _names = names; }
public IEnumerator GetEnumerator() => new PersonsEnumerator(this);
}
public class PersonsEnumerator : IEnumerator {
private Persons _p; private int _index = -1;
public PersonsEnumerator(Persons p) { _p = p; }
public bool MoveNext() => ++_index < _p._names.Length;
public void Reset() => _index = -1;
public object Current => _p._names[_index];
}
IEnumerator e = arrPersons.GetEnumerator();
while (e.MoveNext()) Console.WriteLine((string)e.Current);
public class Persons : IEnumerable {
private string[] _names;
public Persons(params string[] names) { _names = names; }
public IEnumerator GetEnumerator() {
foreach (var s in _names) yield return s;
}
}
極大簡化迭代器實作,編譯器自動生成狀態機代碼。
迭代器模式分離集合遍歷行為,封裝於專責迭代器類別,實現集合與演算法解耦。.NET 透過 IEnumerable/IEnumerator 介面與 yield return 語法提供一級公民支援,開發者應善用框架機制而非重複造輪子。