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

简介: 原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)[索引页][源码下载]乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:webabcd 介绍 提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
原文: 乐在其中设计模式(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;
        
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条信息


参考
http://www.dofactory.com/Patterns/PatternIterator.aspx


OK
[源码下载]
目录
相关文章
|
7月前
|
设计模式 存储 JavaScript
【设计模式】【行为型模式】迭代器模式(Iterator)
一、入门 什么是迭代器模式? 迭代器模式(Iterator Pattern)是一种行为设计模式,它提供了一种顺序访问聚合对象中元素的方法,而不需要暴露其底层表示。迭代器模式将遍历逻辑从聚合对象中分离出
218 11
|
9月前
|
设计模式 Java 数据安全/隐私保护
Java 设计模式:装饰者模式(Decorator Pattern)
装饰者模式属于结构型设计模式,允许通过动态包装对象的方式为对象添加新功能,提供比继承更灵活的扩展方式。该模式通过组合替代继承,遵循开闭原则(对扩展开放,对修改关闭)。
|
设计模式 Java Kotlin
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
132 2
|
设计模式 Java 开发者
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
本教程详细讲解Kotlin语法,适合希望深入了解Kotlin的开发者。对于快速学习Kotlin的用户,推荐查看“简洁”系列教程。本文重点介绍迭代器模式,通过具体示例展示了如何在Kotlin中实现迭代器模式,包括使用Iterator、Iterable接口及重载iterator运算符的方法。
118 4
|
设计模式 Java 开发者
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
本教程详细讲解了Kotlin中的迭代器模式,包括如何通过实现Iterator和Iterable接口以及重载iterator运算符来实现可遍历的自定义集合。示例展示了如何创建一个图书集类,并通过不同方式使其支持遍历操作,适合希望深入了解Kotlin迭代器模式的开发者。
171 3
|
设计模式 Java Kotlin
Kotlin学习笔记 - 改良设计模式 - 迭代器模式
Kotlin学习笔记 - 改良设计模式 - 迭代器模式
127 2
|
设计模式 安全 Java
C# 一分钟浅谈:设计模式之单例模式
【10月更文挑战第9天】单例模式是软件开发中最常用的设计模式之一,旨在确保一个类只有一个实例,并提供一个全局访问点。本文介绍了单例模式的基本概念、实现方式(包括饿汉式、懒汉式和使用 `Lazy&lt;T&gt;` 的方法)、常见问题(如多线程和序列化问题)及其解决方案,并通过代码示例详细说明了这些内容。希望本文能帮助你在实际开发中更好地应用单例模式,提高代码质量和可维护性。
526 1
|
设计模式 Java Kotlin
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
112 1
|
设计模式 Java Kotlin
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
Kotlin教程笔记(54) - 改良设计模式 - 迭代器模式
95 1
|
设计模式 Java Kotlin
Kotlin - 改良设计模式 - 迭代器模式
Kotlin - 改良设计模式 - 迭代器模式
88 0