迭代器模式(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)的更多相关文章

  1. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  2. 二十四种设计模式:观察者模式(Observer Pattern)

    观察者模式(Observer Pattern) 介绍定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 示例有一个Message实体类,某些对象 ...

  3. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  4. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  5. 设计模式 - 迭代器模式(iterator pattern) 具体解释

    迭代器模式(iterator pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 迭代器模式(iterator pattern) : 提供一 ...

  6. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  7. 二十四种设计模式:解释器模式(Interpreter Pattern)

    解释器模式(Interpreter Pattern) 介绍给定一个语言, 定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子. 示例有一个Message实体类,某个类对它的 ...

  8. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  9. 二十四种设计模式:组合模式(Composite Pattern)

    组合模式(Composite Pattern) 介绍将对象组合成树形结构以表示"部分-整体"的层次结构.它使得客户对单个对象和复合对象的使用具有一致性.示例有一个Message实体 ...

随机推荐

  1. spring boot&&cloud干货系列

    接触spring boot也有些时日了,刚开始博主还想参照官方参考指南自己写一个系列的入门式的教程,包含spring boot的所有模块的用法,后来发现,有一大波优秀的系列文章和项目了,所以就没班门弄 ...

  2. linux命令(50):top命令

    TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...

  3. Bootstrap Div 居中的方法

    有两个DIV,DIV2被包含在DIV1中.格式如下: <div id="div1"> <div id="div2">         & ...

  4. Docker 常用命令收集

    查看Docker版本 docker version 查看 Image docker images 打包 Image docker save -o ‘packageName.tar’ ‘imageNam ...

  5. java.util.regex包下的Pattern和Matcher详解(正则匹配)

    java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现(建议在阅读本文时,打开java API文档,当介绍到哪个方法时,查看java API中的方法说明,效果 ...

  6. 使用 ceph 作为 openstack 的后端

    openstack 与 ceph 集成 在 ceph 上创建 openstack 需要的 pool. sudo ceph osd pool create volumes 128 sudo ceph o ...

  7. [mysql] 删除唯一约束unique

    alter table ot_document drop index title

  8. 叙Windows平台下基于MBR和UEFI的bootkit(一)--以MBR为例

    安全的对抗首先在权限方面,权限高的进程对权限低的权限就是就是降维打击,无往不利.当权限相同时,启动得早便为王.所谓的bootkit也就是基于这个思路设计的一种复杂病毒.它优先于Windows系统启动, ...

  9. 查看所有shell类型

    [xf@xuexi ~]$ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/no ...

  10. sqlldr load UTF8 error

    The default length semantics for all datafiles (except UFT-16) is byte. So in your case you have a C ...