12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace IteratorPattern
- {
- // 抽象聚合类
- public interface IListCollection
- {
- Iterator GetIterator();
- }
- // 迭代器抽象类
- public interface Iterator
- {
- bool MoveNext();
- Object GetCurrent();
- void Next();
- void Reset();
- }
- // 具体聚合类
- public class ConcreteList : IListCollection
- {
- readonly string[] _collection;
- public ConcreteList()
- {
- _collection = new string[] { "A", "B", "C", "D" };
- }
- public Iterator GetIterator()
- {
- return new ConcreteIterator(this);
- }
- public int Length
- {
- get { return _collection.Length; }
- }
- public string GetElement(int index)
- {
- return _collection[index];
- }
- }
- // 具体迭代器类
- public class ConcreteIterator : Iterator
- {
- // 迭代器要集合对象进行遍历操作,自然就需要引用集合对象
- private ConcreteList _list;
- private int _index;
- public ConcreteIterator(ConcreteList list)
- {
- _list = list;
- _index = 0;
- }
- public bool MoveNext()
- {
- if (_index < _list.Length)
- {
- return true;
- }
- return false;
- }
- public Object GetCurrent()
- {
- return _list.GetElement(_index);
- }
- public void Reset()
- {
- _index = 0;
- }
- public void Next()
- {
- if (_index < _list.Length)
- {
- _index++;
- }
- }
- }
-
- }
|