二十四种设计模式:迭代器模式(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)的更多相关文章
- 二十四种设计模式:适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...
- 二十四种设计模式:观察者模式(Observer Pattern)
观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- 二十四种设计模式:单例模式(Singleton Pattern)
单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...
- 设计模式 - 迭代器模式(iterator pattern) 具体解释
迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...
- 二十四种设计模式:命令模式(Command Pattern)
命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...
- 二十四种设计模式:解释器模式(Interpreter Pattern)
解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...
- 二十四种设计模式:策略模式(Strategy Pattern)
策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...
- 二十四种设计模式:组合模式(Composite Pattern)
组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...
随机推荐
- Laravel artisan commands
使用php artisan list 可以看到artisan的所有命令以及选项. 当然你也可以在此基础上扩展自己的命令. 1. key 1.1 key:generate 这是一个加密秘钥,用于保证安全 ...
- 使用js获取url里的指定参数
String.prototype.getQuery = function(name){ var reg = new RegExp("(^|&)"+ name +&q ...
- Python在线教程
Python 3.x的 http://www.ziqiangxuetang.com/python3/python3-stdlib.html 廖雪峰的官方网站 http://www.liaoxuefen ...
- Zabbix历史数据库迁移 及分区
https://blog.csdn.net/hkyw000/article/details/78971201?utm_source=blogxgwz6
- zabbix通过jvm监控tomcat
说明:zabbix是通过jvm工具监控tomcat,zabbix server通过连接jvm代理服务器获取tomcat的各种参数 zabbix server:192.168.1.31 tomcat服务 ...
- 【剑指offer】面试题 49. 丑数
面试题 49. 丑数 题目描述 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺 ...
- Maven 管理的WEB项目发布到Tomcat上
1.需要Tomcat服务器 这里可以使用已下载好的Tomcat也可以使用Maven来自动引入Tomcat插件. 通过Maven引入Tomcat服务器 在项目的pom.xml文件中project 标签中 ...
- mongoDB学习第一天之增删改查
mongoDB 是 no-sql 的一种数据库. 创建数据库: use dbName #数据库中如果存在 dbName ,切换到此数据库:如果不存在此数据库,则创建 dbName 数据库!(tip:当 ...
- javascript 的回调函数
既然函数可以像其他数据那样赋值给某个个变量,可以被定义.删除.拷贝,那为什么就不能被当成参数传递给其他函数呢? 下面的示例中,我们定义了一个以两个函数为参数的函数.该函数会分别执行这两个参数函数,并返 ...
- CodeForces 740C Alyona and mex
构造. 比较骚的构造题.肯定可以构造出$min(R-L+1)$,只要$0$ $1$ $2$ $...$ $R-L$ $0$ $1$ $2$ $...$ $R-L$填数字即可,这样任意一段区间都包含了$ ...