行为型设计模式之迭代器模式(Iterator)
| 结构 | ![]() |
| 意图 | 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示。 |
| 适用性 |
|
using System;
using System.Collections; class Node
{
private string name;
public string Name
{
get
{
return name;
}
}
public Node(string s)
{
name = s;
}
} class NodeCollection
{
private ArrayList list = new ArrayList();
private int nodeMax = ; // left as a student exercise - implement collection
// functions to remove and edit entries also
public void AddNode(Node n)
{
list.Add(n);
nodeMax++;
}
public Node GetNode(int i)
{
return ((Node) list[i]);
} public int NodeMax
{
get
{
return nodeMax;
}
}
} /*
* The iterator needs to understand how to traverse the collection
* It can do that as way it pleases - forward, reverse, depth-first,
*/
abstract class Iterator
{
abstract public Node Next();
} class ReverseIterator : Iterator
{
private NodeCollection nodeCollection;
private int currentIndex; public ReverseIterator (NodeCollection c)
{
nodeCollection = c;
currentIndex = c.NodeMax -; // array index starts at 0!
} // note: as the code stands, if the collection changes,
// the iterator needs to be restarted
override public Node Next()
{
if (currentIndex == -)
return null;
else
return(nodeCollection.GetNode(currentIndex--));
}
} /// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
public static int Main(string[] args)
{
NodeCollection c = new NodeCollection();
c.AddNode(new Node("first"));
c.AddNode(new Node("second"));
c.AddNode(new Node("third")); // now use iterator to traverse this
ReverseIterator i = new ReverseIterator(c); // the code below will work with any iterator type
Node n;
do
{
n = i.Next();
if (n != null)
Console.WriteLine("{0}", n.Name);
} while (n != null); return ;
}
}
迭代器模式
行为型设计模式之迭代器模式(Iterator)的更多相关文章
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 二十四种设计模式:迭代器模式(Iterator Pattern)
迭代器模式(Iterator Pattern) 介绍提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示. 示例有一个Message实体类,某聚合对象内的各个元素均为该实体对象,现 ...
- 设计模式之迭代器模式(Iterator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- [设计模式] 16 迭代器模式 Iterator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对迭代器模式是这样说的:提供一种方法顺序访问一个聚合对象中各个元素,而又不需要暴露该对象的内部表示. 类图和实例: 迭代器模式由以下角 ...
- 设计模式 笔记 迭代器模式 Iterator
//---------------------------15/04/26---------------------------- //Iterator 迭代器模式----对象行为型模式 /* 1:意 ...
- 设计模式之迭代器模式 Iterator
代码实现 public interface MyIterator { void first(); //将游标指向第一个元素 void next(); //将游标指向下一个元素 boolean hasN ...
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型) 1.概述 类中的面向对象编程封装应用逻辑.类,就是实例化的对象,每个单独的对象都有一个特定的身份和状态.单独的对象是一种组织代码的 ...
- 迭代器模式 Iterator 行为型 设计模式(二十)
迭代器模式(Iterator) 走遍天下,世界那么大,我想去看看 在计算机中,Iterator意为迭代器,迭代有重复的含义,在程序中,更有“遍历”的含义 如果给定一个数组,我们可以通过for循 ...
随机推荐
- Mysql:case when then end 的用法
0.创建一张数据表 表名为 test_when_case CREATE TABLE `test_when_case` ( `id` int(11) unsigned NOT NULL AUTO_INC ...
- 将Komodo Edit打造成Python开发的IDE
Komodo Edit 支持Python 界面清爽, 将Komodo Edit 设置成Python的IDE,具体操作方法如下: 先添加自定义命令. 再设置命令行参数 设置高级选项 设置快捷键 完成.
- Numpy安装报错:试过N种安装方法终于
Import numpy时,会报下面的错误 /home/spyros/.local/lib/python2.7/site-packages/numpy/core/multiarray.so: unde ...
- @ApiModelProperty用法
@ApiModelProperty()用于方法,字段: 表示对model属性的说明或者数据操作更改 value–字段说明 name–重写属性名字 dataType–重写属性类型 required–是否 ...
- 笔记-twisted
笔记-twisted 1. 简介 Twisted is an event-driven networking engine written in Python and licensed un ...
- Javascript 属性高级写法
http://www.cnblogs.com/YuanSong/p/3899287.html
- postman与charles的结合使用
1.准备charles环境 Charles端口一般配置的为8888,不知道怎么配置详见charles文档 打开charles,发现访问浏览器任意页面都是失败. 在浏览器的高级设置中设置代理服务器,以火 ...
- centos6 install cobbler
cobbler 安装 一:定义yum源 wget -c -O CentOS-Base.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo ...
- codebolocks 中文使用手册1.1
Code::Blocks手册 使用篇 中文翻译版- 原手册下载:http://www.codeblocks.org/docs/manual_en.pdf 译者:JGood 译者言:工欲善其事,必先利其 ...
- springboot13 Hikari 和Introspector
SpringBoot Initializr Introspector(内省) class TestReflect { @Test fun testReflect() { //获取字节码对象 val c ...
