一.集合的内部结构和外部访问
二.动机(Motivation)
在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供了可能。
使用面向对象技术将这种遍历机制抽象为“迭代器 对象”为“应对变化中的集合对象”提供了一种优雅的方式.
三.意图(Intent)
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。-----《设计模式》GoF
四.Iterator模式的几个要点
迭代抽象:访问一个聚合对象的内容而无需暴露它的内部表示。
迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。
迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。
五.结构
六.实例代码
要实现迭代器需要实现的2个接口:
C#1.1中的实现:
二.动机(Motivation)
在软件构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供了可能。
使用面向对象技术将这种遍历机制抽象为“迭代器 对象”为“应对变化中的集合对象”提供了一种优雅的方式.
三.意图(Intent)
提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。-----《设计模式》GoF
四.Iterator模式的几个要点
迭代抽象:访问一个聚合对象的内容而无需暴露它的内部表示。
迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。
迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。
五.结构
六.实例代码
要实现迭代器需要实现的2个接口:
public interface IEnuerable
{
IEnumerator GetEnumerator();
}
public interface IEnumator
{
Object current { get;}
bool MoveNext();
void Reset();
}
C#1.1和C#2.0的迭代器有不同的实现,但实质是一样的。{
IEnumerator GetEnumerator();
}
public interface IEnumator
{
Object current { get;}
bool MoveNext();
void Reset();
}
C#1.1中的实现:
public class MyCollection : IEnumerable
{
int[] items;
public MyCollection()
{
items = new int[5] { 12, 33, 44, 55, 66 };
}
public IEnumerator GetEnumerator()
{ return new MyEnumerator(this); }
private class MyEnumerator : IEnumerator
{
int nIndex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nIndex = -1;
}
public bool MoveNext()
{
nIndex++;
return (nIndex < collection.items.GetLength(0));
}
public object Current
{
get
{
return (collection.items[nIndex]);
}
}
public void Reset()
{
nIndex = -1;
}
}
}
{
int[] items;
public MyCollection()
{
items = new int[5] { 12, 33, 44, 55, 66 };
}
public IEnumerator GetEnumerator()
{ return new MyEnumerator(this); }
private class MyEnumerator : IEnumerator
{
int nIndex;
MyCollection collection;
public MyEnumerator(MyCollection coll)
{
collection = coll;
nIndex = -1;
}
public bool MoveNext()
{
nIndex++;
return (nIndex < collection.items.GetLength(0));
}
public object Current
{
get
{
return (collection.items[nIndex]);
}
}
public void Reset()
{
nIndex = -1;
}
}
}
public class AppCharles
{
public static void Main()
{
MyCollection col = new MyCollection();
foreach (int c in col)
{
Console.WriteLine(c);
}
}
}
{
public static void Main()
{
MyCollection col = new MyCollection();
foreach (int c in col)
{
Console.WriteLine(c);
}
}
}
上面的foreach语句实际上是:
//完全抽象于(不依赖)具体的集合结构的访问操作
IEnumerator ator = col.GetEnumerator();
while (ator.MoveNext())
{
int i = (int)ator.Current;
//ator.Current=100;可以,但C#禁止------更改结构
//ator.Remove(i);绝对禁止--------更改结构
//i=100;//没有更改的效果
Console.WriteLine(i);
}
IEnumerator ator = col.GetEnumerator();
while (ator.MoveNext())
{
int i = (int)ator.Current;
//ator.Current=100;可以,但C#禁止------更改结构
//ator.Remove(i);绝对禁止--------更改结构
//i=100;//没有更改的效果
Console.WriteLine(i);
}
C#2.0的实现:
public class Stack : IEnumerable
{
int[] items;
public Stack()
{
items = new int[5] { 11, 22, 33, 44, 55 };
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < 5; i++)
{
yield return items[i];
}
}
}
{
int[] items;
public Stack()
{
items = new int[5] { 11, 22, 33, 44, 55 };
}
public IEnumerator GetEnumerator()
{
for (int i = 0; i < 5; i++)
{
yield return items[i];
}
}
}
用ildasm工具查看il代码如下:可以看出编译器层面上实际上同C#1.1那样在后台动态添加一个内部类,只是在表面层上省略了让编译器帮我们来实现(实际上也是动态的生成一个内部类)
。
补充:
迭代器器中的yield语句
1.使用yield return 产生枚举元素
for (int i = count - 1; i >= 0; --i)
{
yield return items[i];
}
{
yield return items[i];
}
2.使用yield break中断迭代
for (int i = count - 1; i >= 0; --i)
{
yield return items[i];
if (items[i] > 10)
yield break;
}
{
yield return items[i];
if (items[i] > 10)
yield break;
}
推荐资源
* 《设计模式:可复用面向对象软件的基础》GoF
* 《面向对象分析与设计》Grady Booch
* 《敏捷软件开发:原则、模式与实践》Robert C. Martin
* 《重构:改善既有代码的设计》Martin Fowler
* 《Refactoring to Patterns》Joshua Kerievsky
更多MSDN资源
* MSDN中文网站
http://www.microsoft.com/china/msdn
* MSDN中文网络广播
http://www.msdnwebcast.com.cn
* MSDN Flash
http://www.microsoft.com/china/newsletter/case/msdn.aspx
* MSDN开发中心
http://www.microsoft.com/china/msdn/DeveloperCenter/default.mspx