行为型设计模式之迭代器模式(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循 ...
随机推荐
- selenium库:自动化测试工具
爬虫中主要用来解决Javascript渲染问题 1.声明浏览器对象: from selenium import webdriver browser = webdriver.浏览器名() 2.访问页面: ...
- C语言字符篇(一)字符串转换函数
#include <stdlib.h> double atof(const char *nptr); 将字符串转换成双精度浮点数 int atoi(const char *npt ...
- Choosing Capital for Treeland CodeForces - 219D (树形DP)
传送门 The country Treeland consists of n cities, some pairs of them are connected with unidirectional ...
- POJ 3608 凸包间最短距离(旋转卡壳)
Bridge Across Islands Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11539 Accepted: ...
- Codeforces Round #449 (Div. 2) C. DFS
C. Nephren gives a riddle time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- 边缘检测 opencv
本次实验使用了两种方法进行了边缘检测,分别使用到了opencv中的两个API函数为Canny()和Sobel()函数.实验后加了Scharr滤波器,它其实是基于Sobel()函数的. 这三个API中的 ...
- 使用postMan测试insert或者update接口
URL : http://localhost:8099/orderVoice/updateAgentLogin?access_token=7f10e803-f886-47df-b3dc-9ed307d ...
- [Link-Cut-Tree][BZOJ2631]Tree
题面 Description: 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\(v\)的路径上的点的 ...
- dialog BLE SDK 学习(3)
dialog DA14580 SDK版本:5.0.4. 本文介绍了DA14580 SDK的学习感想和建议,分享给大家. 首先,Dialog官网上的资料挺全的,但是是英文的,如果英文不好,比如笔者,阅读 ...
- vue命令集合
创建vuecli脚手架:npm install -g @vue/cli拉取2的版本:npm install -g @vue/cli-init 创建webpack:npm i webpack@3.12. ...