设计模式(C#)之迭代器模式(Iterator Pattern)
1.概念
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
2.类图
3.代码
Model.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
public class Model
{
public string ID
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
ICollection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
public interface ICollection
{
/// <summary>
/// 得到迭代对象
/// </summary>
/// <returns></returns>
Iiterator GetIterator();
}
}
Collection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
public class Collection : ICollection
{
private List<Model> list = new List<Model>();
/// <summary>
/// 创建迭代器对象
/// </summary>
/// <returns></returns>
public Iiterator GetIterator()
{
return new Iterator(this);
}
/// <summary>
/// 集合内的对象总数
/// </summary>
public int Count
{
get { return list.Count; }
}
/// <summary>
/// 索引器
/// </summary>
/// <param name="index">index</param>
/// <returns></returns>
public Model this[int index]
{
get { return list[index]; }
set { list.Add(value); }
}
}
}
Iiterator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace IteratorPattern
{
public interface Iiterator
{
Model Current { get; }
Model MoveNext();
Model First();
}
}
Iterator.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IteratorPattern
{
public class Iterator : Iiterator
{
private Collection _collection;
private int _current = 0;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="collection"></param>
public Iterator(Collection collection)
{
this._collection = collection;
}
/// <summary>
/// 第一个对象
/// </summary>
/// <returns></returns>
public Model First()
{
_current = 0;
return _collection[_current];
}
/// <summary>
/// 下一个对象
/// </summary>
/// <returns></returns>
public Model MoveNext()
{
_current += 1;
if (!IsEnd)
{
return _collection[_current];
}
else
{
return null;
}
}
/// <summary>
/// 是否迭代完毕
/// </summary>
public bool IsEnd
{
get { return _current >= _collection.Count ? true : false; }
}
/// <summary>
/// 当前
/// </summary>
public Model Current
{
get { return _collection[_current]; }
}
}
}
测试代码
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace IteratorPattern
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Collection collection = new Collection();
collection[0] = new Model() { ID = "0", Name = "User1" };
collection[1] = new Model() { ID = "1", Name = "User2" };
collection[2] = new Model() { ID = "2", Name = "User3" };
collection[3] = new Model() { ID = "3", Name = "User4" };
collection[4] = new Model() { ID = "4", Name = "User5" };
collection[5] = new Model() { ID = "5", Name = "User6" };
Iterator iterator = new Iterator(collection);
for (Model mm = iterator.First(); !iterator.IsEnd; mm = iterator.MoveNext())
{
this.listBox1.Items.Add("编号:" + mm.ID + "名称:" + mm.Name);
}
}
}
}
4.测试结果