设计模式(C#)之迭代器模式(Iterator Pattern)
来源:互联网 发布:excel表格自动生成数据 编辑:程序博客网 时间:2024/05/17 19:14
设计模式(C#)之迭代器模式(Iterator Pattern)
代码下载
1.概念
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
2.类图
3.代码
Model.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace IteratorPattern
- {
- public class Model
- {
- public string ID
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- }
- }
ICollection.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace IteratorPattern
- {
- public interface ICollection
- {
- /// <summary>
- /// 得到迭代对象
- /// </summary>
- /// <returns></returns>
- Iiterator GetIterator();
- }
- }
Collection.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace IteratorPattern
- {
- public class Collection : ICollection
- {
- private List<Model> list = new List<Model>();
- /// <summary>
- /// 创建迭代器对象
- /// </summary>
- /// <returns></returns>
- public Iiterator GetIterator()
- {
- return new Iterator(this);
- }
- /// <summary>
- /// 集合内的对象总数
- /// </summary>
- public int Count
- {
- get { return list.Count; }
- }
- /// <summary>
- /// 索引器
- /// </summary>
- /// <param name="index">index</param>
- /// <returns></returns>
- public Model this[int index]
- {
- get { return list[index]; }
- set { list.Add(value); }
- }
- }
- }
Iiterator.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Collections;
- namespace IteratorPattern
- {
- public interface Iiterator
- {
- Model Current { get; }
- Model MoveNext();
- Model First();
- }
- }
Iterator.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace IteratorPattern
- {
- public class Iterator : Iiterator
- {
- private Collection _collection;
- private int _current = 0;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="collection"></param>
- public Iterator(Collection collection)
- {
- this._collection = collection;
- }
- /// <summary>
- /// 第一个对象
- /// </summary>
- /// <returns></returns>
- public Model First()
- {
- _current = 0;
- return _collection[_current];
- }
- /// <summary>
- /// 下一个对象
- /// </summary>
- /// <returns></returns>
- public Model MoveNext()
- {
- _current += 1;
- if (!IsEnd)
- {
- return _collection[_current];
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 是否迭代完毕
- /// </summary>
- public bool IsEnd
- {
- get { return _current >= _collection.Count ? true : false; }
- }
- /// <summary>
- /// 当前
- /// </summary>
- public Model Current
- {
- get { return _collection[_current]; }
- }
- }
- }
测试代码
Form1.cs
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace IteratorPattern
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- Collection collection = new Collection();
- collection[0] = new Model() { ID = "0", Name = "User1" };
- collection[1] = new Model() { ID = "1", Name = "User2" };
- collection[2] = new Model() { ID = "2", Name = "User3" };
- collection[3] = new Model() { ID = "3", Name = "User4" };
- collection[4] = new Model() { ID = "4", Name = "User5" };
- collection[5] = new Model() { ID = "5", Name = "User6" };
- Iterator iterator = new Iterator(collection);
- for (Model mm = iterator.First(); !iterator.IsEnd; mm = iterator.MoveNext())
- {
- this.listBox1.Items.Add("编号:" + mm.ID + "名称:" + mm.Name);
- }
- }
- }
- }
4.测试结果
http://blog.csdn.net/zhaoyu_1979/article/details/7300145
- 设计模式(C#)之迭代器模式(Iterator Pattern)
- 设计模式(C#)之迭代器模式(Iterator Pattern)
- 设计模式之迭代器模式--- Pattern Iterator
- 设计模式之迭代器(Iterator Pattern)-笔记
- 设计模式之-迭代器(Iterator pattern)
- Iterator Pattern--迭代器设计模式
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
- Php设计模式之【迭代器模式Iterator Pattern】
- 设计模式之九:迭代器模式(Iterator Pattern)
- C#设计模式之迭代器模式(Iterator Pattern)
- JAVA设计模式之 迭代器模式【Iterator Pattern】
- 设计模式之迭代器模式(Iterator Pattern)
- 设计模式(行为型)之迭代器模式(Iterator Pattern)
- 设计模式总结之Iterator Pattern(迭代器模式)
- Net设计模式实例之迭代器模式(Iterator Pattern)
- 【23种设计模式】之 迭代器模式(Iterator Pattern)
- 设计模式拾荒之迭代器模式 ( Iterator Pattern )
- 设计模式21:Iterator Pattern (迭代器模式)
- DWR
- JAVA写的仿QQ界面,很早前写的,刚开始弄博客就复制过来了
- emacs cscope浏览代码
- 我想学电路设计,有没有比较好的书
- JAVA对象池
- 设计模式(C#)之迭代器模式(Iterator Pattern)
- 基本功练习_2_29_之(易失变量,中断,异常)
- SCROLLBAR滚动条
- Uva OJ 11205 - The broken pedometer
- 戒指戴法全解
- 360需要的,不是打工者
- 玛丽亚绘图
- ProtoType探索
- Hibernate: No row with the given identifier exists错误