原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

[索引页][源码下载]

乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

作者:webabcd





介绍

提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。





示例

有一个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;

        ;

        ;



        /**//// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="collection"></param>

        public Iterator(Collection collection)

        {

            this._collection = collection;

        }



        /**//// <summary>

        /// 第一个对象

        /// </summary>

        /// <returns></returns>

        public MessageModel First()

        {

            _current ;

            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[] = new I::MessageModel("第1条信息", DateTime.Now);

        collection[] = new I::MessageModel("第2条信息", DateTime.Now);

        collection[] = new I::MessageModel("第3条信息", DateTime.Now);

        collection[] = new I::MessageModel("第4条信息", DateTime.Now);

        collection[] = new I::MessageModel("第5条信息", DateTime.Now);

        collection[] = new I::MessageModel("第6条信息", DateTime.Now);

        collection[] = new I::MessageModel("第7条信息", DateTime.Now);

        collection[] = new I::MessageModel("第8条信息", DateTime.Now);

        collection[] = new I::MessageModel("第9条信息", DateTime.Now);



        I::Iterator iterator = new I::Iterator(collection);



        iterator.Step ;



        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

[源码下载]

乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)的更多相关文章

  1. 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)

    设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...

  2. 二十四种设计模式:迭代器模式(Iterator Pattern)

    迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...

  3. [设计模式] 16 迭代器模式 Iterator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...

  4. 设计模式(十):从电影院中认识"迭代器模式"(Iterator Pattern)

    上篇博客我们从醋溜土豆丝与清炒苦瓜中认识了“模板方法模式”,那么在今天这篇博客中我们要从电影院中来认识"迭代器模式"(Iterator Pattern).“迭代器模式”顾名思义就是 ...

  5. 设计模式系列之迭代器模式(Iterator Pattern)——遍历聚合对象中的元素

    模式概述 模式定义 模式结构图 模式伪代码 模式改进 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修 ...

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

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

  7. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  8. 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

    原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 访问者模式(Visitor Pattern) 作者:webabc ...

  9. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

随机推荐

  1. POJ2599+POJ2082【最大矩形面积】

    题目链接:http://poj.org/problem?id=2559 题目链接:http://poj.org/problem?id=2082 这一类题目的解法,不知自己闲着没事就做了两个. 果然压栈 ...

  2. Graphical Shell with WebShell - WebOS Internals

    Graphical Shell with WebShell - WebOS Internals Graphical Shell with WebShell From WebOS Internals J ...

  3. 【C#遗补】之Char.IsDigit和Char.IsNumber的区别

    原文:[C#遗补]之Char.IsDigit和Char.IsNumber的区别 Char中IsDigit和IsNumber的两个方法都是用来判断字符是否是数字的,那他们有什么区别 IsDigit    ...

  4. Hlg 1832 【线段树 && RMQ】.cpp

    题意: 在给出的区间内求出最大买进卖出的差价. 思路: 对于弱数据:维护一个从左到右的最大差价和最小值.即当发现当前值比最小值小的时候更新最小值,否则看一下当前值与之前最小值的差价是否比最大差价大,是 ...

  5. 面对多个互斥量的加锁策略:"试加锁-回退"算法/固定加锁层次

    有时一个互斥量是不够的: 比如: 当多个线程同时访问一个队列结构时,你需要2个互斥量,一个用来保护队列头,一个用来保护队列元素内的数据. 当为多线程建立一个树结构时,你可能需要为每个节点设置一个互斥量 ...

  6. hdu3974(线段树+dfs)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属.都停下手上的工作,开始做a. ...

  7. 创建和关联内容数据库到指定Web应用程序和站点集

    创建和关联内容数据库到指定Web应用程序和站点集         一个Web应用程序不限于使用单个内容数据库.SharePoint同意你关联多个内容数据库到Web应用程序.原因之中的一个是基于内容数据 ...

  8. c++进阶

    对网络编程/多线程/系统编程有一定了解:4:对ngnix,redis,memcache有一定了解:5:有高并发服务开发经验优先: 因为C/C++在嵌入式.移动互联网.物联网有很大的优势,有很多人就靠一 ...

  9. zabbix 监控特定进程

    因为一些server上跑着一些重要程序,须要对它们进行监控,公司用的是zabbix监控,之前都是在zabbix中加入自己定义脚本对特定程序进行监控,近期看了zabbix的官方文档,发现原来强大的zab ...

  10. 一篇哥们自己的写的IBM电话面试感想-@大国

    两天没写博了,还是没有养成一个习惯.前天和昨天休息,和哥们几个出去打球,运动一下,放松下脑子.今天就补一篇我哥们自己的写的关于他的IBM电话面试的感想,希望能帮到有需要的人. ------------ ...