首页 > 其他 > 详细

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

时间:2014-03-09 21:17:26      阅读:556      评论:0      收藏:0      [点我收藏+]
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

[索引页]
[源码下载]


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


作者:webabcd


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


示例
有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现在要提供一种方法顺序地访问这个聚合对象中的各个元素。
bubuko.com,布布扣


MessageModel
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Iterator
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Message实体类
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class MessageModel
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="msg">Message内容</param>
bubuko.com,布布扣        
/// <param name="pt">Message发布时间</param>

bubuko.com,布布扣        public MessageModel(string msg, DateTime pt)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
this._message = msg;
bubuko.com,布布扣            
this._publishTime = pt;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private string _message;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message内容
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public string Message
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _message; }
bubuko.com,布布扣            
set { _message = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private DateTime _publishTime;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message发布时间
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public DateTime PublishTime
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _publishTime; }
bubuko.com,布布扣            
set { _publishTime = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

ICollection
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Iterator
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 集合接口(Aggregate)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public interface ICollection
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 创建迭代器对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        IIterator CreateIterator();
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

Collection
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Iterator
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 集合(ConcreteAggregate)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class Collection : ICollection
bubuko.com,布布扣    
{
bubuko.com,布布扣        
private List<MessageModel> list = new List<MessageModel>();
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 创建迭代器对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public IIterator CreateIterator()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
return new Iterator(this);
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 集合内的对象总数
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public int Count
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return list.Count; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 索引器
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="index">index</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public MessageModel this[int index]
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return list[index]; }
bubuko.com,布布扣            
set { list.Add(value); }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

IIterator
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Iterator
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 迭代器接口(IIterator)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public interface IIterator
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 第一个对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        MessageModel First();
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 下一个对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        MessageModel Next();
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 当前对象
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        MessageModel CurrentMessageModel get; }
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 是否迭代完毕
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        bool IsDone get; }
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

Iterator
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Iterator
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 迭代器(Iterator)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class Iterator : IIterator
bubuko.com,布布扣    
{
bubuko.com,布布扣        
private Collection _collection;
bubuko.com,布布扣        
private int _current = 0;
bubuko.com,布布扣        
private int _step = 1;
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="collection"></param>

bubuko.com,布布扣        public Iterator(Collection collection)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
this._collection = collection;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 第一个对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public MessageModel First()
bubuko.com,布布扣        
{
bubuko.com,布布扣            _current 
= 0;
bubuko.com,布布扣            
return _collection[_current];
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 下一个对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public MessageModel Next()
bubuko.com,布布扣        
{
bubuko.com,布布扣            _current 
+= _step;
bubuko.com,布布扣
bubuko.com,布布扣            
if (!IsDone)
bubuko.com,布布扣            
{
bubuko.com,布布扣                
return _collection[_current];
bubuko.com,布布扣            }

bubuko.com,布布扣            
else
bubuko.com,布布扣            
{
bubuko.com,布布扣                
return null;
bubuko.com,布布扣            }

bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 当前对象
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public MessageModel CurrentMessageModel
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _collection[_current]; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 是否迭代完毕
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public bool IsDone
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _current >= _collection.Count ? true : false; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 步长
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public int Step
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _step; }
bubuko.com,布布扣            
set { _step = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣


Test
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Data;
bubuko.com,布布扣
using System.Configuration;
bubuko.com,布布扣
using System.Collections;
bubuko.com,布布扣
using System.Web;
bubuko.com,布布扣
using System.Web.Security;
bubuko.com,布布扣
using System.Web.UI;
bubuko.com,布布扣
using System.Web.UI.WebControls;
bubuko.com,布布扣
using System.Web.UI.WebControls.WebParts;
bubuko.com,布布扣
using System.Web.UI.HtmlControls;
bubuko.com,布布扣
bubuko.com,布布扣
using I = Pattern.Iterator;
bubuko.com,布布扣
bubuko.com,布布扣
public partial class Iterator : System.Web.UI.Page
bubuko.com,布布扣
{
bubuko.com,布布扣    
protected void Page_Load(object sender, EventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        I::Collection collection 
= new I::Collection();
bubuko.com,布布扣
bubuko.com,布布扣        collection[
0= new I::MessageModel("第1条信息", DateTime.Now);
bubuko.com,布布扣        collection[
1= new I::MessageModel("第2条信息", DateTime.Now);
bubuko.com,布布扣        collection[
2= new I::MessageModel("第3条信息", DateTime.Now);
bubuko.com,布布扣        collection[
3= new I::MessageModel("第4条信息", DateTime.Now);
bubuko.com,布布扣        collection[
4= new I::MessageModel("第5条信息", DateTime.Now);
bubuko.com,布布扣        collection[
5= new I::MessageModel("第6条信息", DateTime.Now);
bubuko.com,布布扣        collection[
6= new I::MessageModel("第7条信息", DateTime.Now);
bubuko.com,布布扣        collection[
7= new I::MessageModel("第8条信息", DateTime.Now);
bubuko.com,布布扣        collection[
8= new I::MessageModel("第9条信息", DateTime.Now);
bubuko.com,布布扣
bubuko.com,布布扣        I::Iterator iterator 
= new I::Iterator(collection);
bubuko.com,布布扣
bubuko.com,布布扣        iterator.Step 
= 2;
bubuko.com,布布扣
bubuko.com,布布扣        
for (I::MessageModel mm = iterator.First(); !iterator.IsDone; mm = iterator.Next())
bubuko.com,布布扣        
{
bubuko.com,布布扣            Response.Write(mm.Message);
bubuko.com,布布扣            Response.Write(
"<br />");
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

运行结果
第1条信息
第3条信息
第5条信息
第7条信息
第9条信息


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


OK
[源码下载]

乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern),布布扣,bubuko.com

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

原文:http://www.cnblogs.com/lonelyxmas/p/3590355.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!