返回“設計模式(C#)系列文章索引”
介紹
提供一種方法順序訪問一個聚合對象中各個元素,而又不需暴露該對象的內部表示。
示例
有一個Message實體類,某聚合對象內的各個元素均為該實體對象,現在要提供一種方法順序地訪問這個聚合對象中的各個元素。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Iterator
{
/**//// <summary>
/// Message實體類
/// </summary>
public class MessageModel
{
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="msg">Message內容</param>
/// <param name="pt">Message發布時間</param>
public MessageModel(string msg, DateTime pt)
{
this._message = msg;
this._publishTime = pt;
}
private string _message;
/**//// <summary>
/// Message內容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message發布時間
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
ICollection
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Iterator
{
/**//// <summary>
/// 集合接口(Aggregate)
/// </summary>
public interface ICollection
{
/**//// <summary>
/// 創建迭代器對象
/// </summary>
/// <returns></returns>
IIterator CreateIterator();
}
}
Collection
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Iterator
{
/**//// <summary>
/// 集合(ConcreteAggregate)
/// </summary>
public class Collection : ICollection
{
private List<MessageModel> list = new List<MessageModel>();
/**//// <summary>
/// 創建迭代器對象
/// </summary>
/// <returns></returns>
public IIterator CreateIterator()
{
return new Iterator(this);
}
/**//// <summary>
/// 集合內的對象總數
/// </summary>
public int Count
{
get { return list.Count; }
}
/**//// <summary>
/// 索引器
/// </summary>
/// <param name="index">index</param>
/// <returns></returns>
public MessageModel this[int index]
{
get { return list[index]; }
set { list.Add(value); }
}
}
}
IIterator
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Iterator
{
/**//// <summary>
/// 迭代器接口(IIterator)
/// </summary>
public interface IIterator
{
/**//// <summary>
/// 第一個對象
/// </summary>
/// <returns></returns>
MessageModel First();
/**//// <summary>
/// 下一個對象
/// </summary>
/// <returns></returns>
MessageModel Next();
/**//// <summary>
/// 當前對象
/// </summary>
MessageModel CurrentMessageModel { get; }
/**//// <summary>
/// 是否迭代完畢
/// </summary>
bool IsDone { get; }
}
}
Iterator
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Iterator
{
/**//// <summary>
/// 迭代器(Iterator)
/// </summary>
public class Iterator : IIterator
{
private Collection _collection;
private int _current = 0;
private int _step = 1;
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="collection"></param>
public Iterator(Collection collection)
{
this._collection = collection;
}
/**//// <summary>
/// 第一個對象
/// </summary>
/// <returns></returns>
public MessageModel First()
{
_current = 0;
return _collection[_current];
}
/**//// <summary>
/// 下一個對象
/// </summary>
/// <returns></returns>
public MessageModel Next()
{
_current += _step;
if (!IsDone)
{
return _collection[_current];
}
else
{
return null;
}
}
/**//// <summary>
/// 當前對象
/// </summary>
public MessageModel CurrentMessageModel
{
get { return _collection[_current]; }
}
/**//// <summary>
/// 是否迭代完畢
/// </summary>
public bool IsDone
{
get { return _current >= _collection.Count ? true : false; }
}
/**//// <summary>
/// 步長
/// </summary>
public int Step
{
get { return _step; }
set { _step = value; }
}
}
}
Test
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using I = Pattern.Iterator;
public partial class Iterator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
I::Collection collection = new I::Collection();
collection[0] = new I::MessageModel("第1條信息", DateTime.Now);
collection[1] = new I::MessageModel("第2條信息", DateTime.Now);
collection[2] = new I::MessageModel("第3條信息", DateTime.Now);
collection[3] = new I::MessageModel("第4條信息", DateTime.Now);
collection[4] = new I::MessageModel("第5條信息", DateTime.Now);
collection[5] = new I::MessageModel("第6條信息", DateTime.Now);
collection[6] = new I::MessageModel("第7條信息", DateTime.Now);
collection[7] = new I::MessageModel("第8條信息", DateTime.Now);
collection[8] = new I::MessageModel("第9條信息", DateTime.Now);
I::Iterator iterator = new I::Iterator(collection);
iterator.Step = 2;
for (I::MessageModel mm = iterator.First(); !iterator.IsDone; mm = iterator.Next())
{
Response.Write(mm.Message);
Response.Write("<br />");
}
}
}
運行結果
第1條信息
第3條信息
第5條信息
第7條信息
第9條信息
參考
http://www.dofactory.com/Patterns/PatternIterator.aspx
OK