Iterator迭代器模式



C# Iterator迭代器模式我们在平时的开发中应该经常用到。不直接使用也会间接使用,我们使用foreach语句来循环就是在间接的使用C# Iterator迭代器模式。

迭代器就像指针一样可以向前向后移动,在.NET中迭代器只能向后移动。

动机:在软件的构建过程中,集合对象内部结构常常变化各异。但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码透明地访问其中包含的元素;同时这种“透明遍历”也为“同一种算法在多种集合对象上进行操作”提供了可能。使用面向对象技术将这种遍历机制抽象为“迭代器对象”为“应对变化中的集合对象”提供了一种优雅的方式。

意图:提供一种方法顺序访问一个集合对象中的各个元素,而不暴露该对象的内部表示。

 
 
  1. public interface IEnumerable{  
  2. //得到迭代器  
  3. IEnumerator GetEnumerator();  
  4. }  
  5. /// <summary> 
  6. /// 迭代器接口  
  7. /// summary> 
  8. public interface IEnumerator{  
  9. //得到当前的对象  
  10. object Current{  
  11. get;  
  12. }  
  13. bool MoveNext();  
  14. void Reset();  
  15. }  
  16. /// <summary> 
  17. /// 集合类型,实现了可迭代接口  
  18. /// summary> 
  19. public class MyCollection : IEnumerable{  
  20. internal int[] items;  
  21. public MyCollection(){  
  22. items = new int[5] {1, 2, 3, 4, 5};  
  23. }  
  24. #region IEnumerable 成员  
  25. //实现迭代接口,返回迭代器  
  26. public IEnumerator GetEnumerator(){  
  27. //在这里进行解藕,将集合对象转换为迭代器  
  28. return new MyEnumerator(this);  
  29. }  
  30. #endregion  
  31. }  
  32. //迭代器对象,实现了迭代器接口  
  33. internal class MyEnumerator : IEnumerator{  
  34. private int nIndex;  
  35. MyCollection collection;   
  36. //构造函数将集合类型转换成内部成员  
  37. public MyEnumerator(MyCollection coll){  
  38. this.collection = coll;  
  39. nIndex = -1;  
  40. }  
  41. #region IEnumerator 成员  
  42. //返回当前迭代到的对象  
  43. public object Current{  
  44. get{  
  45. return collection.items[nIndex];  
  46. }  
  47. }  
  48. //移动到下一个对象,指针向后移动  
  49. public bool MoveNext(){  
  50. nIndex++;  
  51. return (nIndex < collection.items.GetLength(0));  
  52. }  
  53. //重设迭代器,指针回零  
  54. public void Reset(){  
  55. nIndex = -1;  
  56. }  
  57. #endregion  
  58. }  

很清楚,在上面的代码中,我们通过GetEnumerator方法,将集合对象转换为了可迭代对象,这实际上是在对集合对象进行抽象,将他转换为迭代器。在这里,我们需要定义一个迭代器类,但是这是.NET 1.1中的做法,在.NET 2.0以后实现一个可迭代模式更加简单。

 
 
  1. /// <summary> 
  2. /// 集合类型,实现了可迭代接口  
  3. /// summary> 
  4. public class MyCollection : IEnumerable<int> 
  5. {  
  6. internal int[] items;  
  7.  
  8. public MyCollection()  
  9. {  
  10. items = new int[5] {1, 2, 3, 4, 5};  
  11. }  
  12.  
  13. #region IEnumerable<int> 成员  
  14.  
  15. public IEnumerator<int> GetEnumerator()  
  16. {  
  17. for(int i = 0; i < items.Length; i++)  
  18. {  
  19. yield return items[i];  
  20. }  
  21. }  
  22. #endregion  
  23.  
  24. #region IEnumerable 成员  
  25.  
  26. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()  
  27. {  
  28. for(int i = 0; i < items.Length; i++)  
  29. {  
  30. yield return items[i];  
  31. }  
  32. }  
  33.  
  34. #endregion  

我们通过yield return关键字来返回一个IEnumerator接口,这个关键在在编译之后会自动生成对应的迭代器的代码。

在.NET中迭代器只能先前,在c++中可以向后等其他操作。

注意:在迭代的过程中,我们不能向集合添加内容,后移除集合里的item,这样将会导致一些问题的出现。以上介绍C# Iterator迭代器模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值