设计模式(C#)之迭代器模式(Iterator Pattern)

来源:互联网 发布:excel表格自动生成数据 编辑:程序博客网 时间:2024/05/17 19:14

设计模式(C#)之迭代器模式(Iterator Pattern)


代码下载

1.概念
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
2.类图

3.代码

Model.cs

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace IteratorPattern  
  7. {  
  8.     public class Model  
  9.     {  
  10.   
  11.         public string ID  
  12.         {  
  13.             get;  
  14.             set;  
  15.   
  16.         }  
  17.   
  18.         public string Name  
  19.         {  
  20.             get;  
  21.             set;  
  22.   
  23.         }  
  24.     }  
  25. }  


 

ICollection.cs

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace IteratorPattern  
  7. {  
  8.     public interface  ICollection  
  9.     {  
  10.         /// <summary>  
  11.         /// 得到迭代对象  
  12.         /// </summary>  
  13.         /// <returns></returns>  
  14.         Iiterator GetIterator();  
  15.     }  
  16. }  

 

Collection.cs

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace IteratorPattern  
  7. {  
  8.     public class Collection : ICollection  
  9.     {  
  10.         private List<Model> list = new List<Model>();  
  11.   
  12.         /// <summary>  
  13.         /// 创建迭代器对象  
  14.         /// </summary>  
  15.         /// <returns></returns>  
  16.         public Iiterator GetIterator()  
  17.         {  
  18.             return new Iterator(this);  
  19.         }  
  20.   
  21.         /// <summary>  
  22.         /// 集合内的对象总数  
  23.         /// </summary>  
  24.         public int Count  
  25.         {  
  26.             get { return list.Count; }  
  27.         }  
  28.   
  29.         /// <summary>  
  30.         /// 索引器  
  31.         /// </summary>  
  32.         /// <param name="index">index</param>  
  33.         /// <returns></returns>  
  34.         public Model this[int index]  
  35.         {  
  36.             get { return list[index]; }  
  37.             set { list.Add(value); }  
  38.         }  
  39.   
  40.     }  
  41. }  


 


Iiterator.cs

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Collections;  
  6.   
  7. namespace IteratorPattern  
  8. {  
  9.     public interface Iiterator  
  10.     {  
  11.          
  12.         Model  Current { get; }  
  13.   
  14.         Model MoveNext();  
  15.   
  16.         Model First();  
  17.     }  
  18. }  


 

Iterator.cs

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace IteratorPattern  
  7. {  
  8.     public class Iterator : Iiterator  
  9.     {  
  10.         private Collection _collection;  
  11.         private int _current = 0;  
  12.   
  13.         /// <summary>  
  14.         /// 构造函数  
  15.         /// </summary>  
  16.         /// <param name="collection"></param>  
  17.         public Iterator(Collection collection)  
  18.         {  
  19.             this._collection = collection;  
  20.         }  
  21.   
  22.         /// <summary>  
  23.         /// 第一个对象  
  24.         /// </summary>  
  25.         /// <returns></returns>  
  26.         public Model First()  
  27.         {  
  28.             _current = 0;  
  29.             return _collection[_current];  
  30.         }  
  31.   
  32.   
  33.         /// <summary>  
  34.         /// 下一个对象  
  35.         /// </summary>  
  36.         /// <returns></returns>  
  37.         public Model MoveNext()  
  38.         {  
  39.             _current += 1;  
  40.   
  41.             if (!IsEnd)  
  42.             {  
  43.                 return _collection[_current];  
  44.             }  
  45.             else  
  46.             {  
  47.                 return null;  
  48.             }  
  49.   
  50.         }  
  51.   
  52.         /// <summary>  
  53.         /// 是否迭代完毕  
  54.         /// </summary>  
  55.         public bool IsEnd  
  56.         {  
  57.             get { return _current >= _collection.Count ? true : false; }  
  58.         }  
  59.   
  60.         /// <summary>  
  61.         /// 当前  
  62.         /// </summary>  
  63.         public Model Current  
  64.         {  
  65.             get { return _collection[_current]; }  
  66.         }  
  67.   
  68.     }  
  69. }  


测试代码

Form1.cs

 

[csharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace IteratorPattern  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         private void button1_Click(object sender, EventArgs e)  
  20.         {  
  21.             Collection collection = new Collection();  
  22.             collection[0] = new Model() { ID = "0", Name = "User1" };  
  23.             collection[1] = new Model() { ID = "1", Name = "User2" };  
  24.             collection[2] = new Model() { ID = "2", Name = "User3" };  
  25.             collection[3] = new Model() { ID = "3", Name = "User4" };  
  26.             collection[4] = new Model() { ID = "4", Name = "User5" };  
  27.             collection[5] = new Model() { ID = "5", Name = "User6" };  
  28.             Iterator iterator = new Iterator(collection);  
  29.             for (Model mm = iterator.First(); !iterator.IsEnd; mm = iterator.MoveNext())  
  30.             {  
  31.                 this.listBox1.Items.Add("编号:" + mm.ID + "名称:" + mm.Name);  
  32.             }  
  33.   
  34.         }  
  35.     }  
  36. }  


4.测试结果


http://blog.csdn.net/zhaoyu_1979/article/details/7300145


原创粉丝点击