迭代器模式(Iterator Pattern)
介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现在要提供一种方法顺序地访问这个聚合对象中的各个元素。MessageModel
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Iterator{ ////// Message实体类 /// public class MessageModel { ////// 构造函数 /// /// Message内容 /// Message发布时间 public MessageModel(string msg, DateTime pt) { this._message = msg; this._publishTime = pt; } private string _message; ////// Message内容 /// public string Message { get { return _message; } set { _message = value; } } private DateTime _publishTime; ////// Message发布时间 /// public DateTime PublishTime { get { return _publishTime; } set { _publishTime = value; } } }}
ICollection
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Iterator{ ////// 集合接口(Aggregate) /// public interface ICollection { ////// 创建迭代器对象 /// ///IIterator CreateIterator(); }}
Collection
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Iterator{ ////// 集合(ConcreteAggregate) /// public class Collection : ICollection { private Listlist = new List (); /// /// 创建迭代器对象 /// ///public IIterator CreateIterator() { return new Iterator(this); } /// /// 集合内的对象总数 /// public int Count { get { return list.Count; } } ////// 索引器 /// /// index ///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{ ////// 迭代器接口(IIterator) /// public interface IIterator { ////// 第一个对象 /// ///MessageModel First(); /// /// 下一个对象 /// ///MessageModel Next(); /// /// 当前对象 /// MessageModel CurrentMessageModel { get; } ////// 是否迭代完毕 /// bool IsDone { get; } }}
Iterator
using System;using System.Collections.Generic;using System.Text;namespace Pattern.Iterator{ ////// 迭代器(Iterator) /// public class Iterator : IIterator { private Collection _collection; private int _current = 0; private int _step = 1; ////// 构造函数 /// /// public Iterator(Collection collection) { this._collection = collection; } ////// 第一个对象 /// ///public MessageModel First() { _current = 0; return _collection[_current]; } /// /// 下一个对象 /// ///public MessageModel Next() { _current += _step; if (!IsDone) { return _collection[_current]; } else { return null; } } /// /// 当前对象 /// public MessageModel CurrentMessageModel { get { return _collection[_current]; } } ////// 是否迭代完毕 /// public bool IsDone { get { return _current >= _collection.Count ? true : false; } } ////// 步长 /// 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(""); } }}
运行结果 第1条信息
第3条信息 第5条信息 第7条信息 第9条信息