首页 > 代码库 > 二十四种设计模式:迭代器模式(Iterator Pattern)
二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern)
介绍
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
示例
有一个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条信息
二十四种设计模式:迭代器模式(Iterator Pattern)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。