设计模式(C#)之迭代器模式(Iterator Pattern)

本文介绍 C# 中的迭代器模式,通过具体实现示例解释如何为集合提供统一的访问接口而不暴露其内部结构。代码示例展示了如何定义集合、迭代器接口及其实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计模式(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.测试结果


代码下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值