Iterator模式C++实现
#include <iostream>
using namespace std;
typedef int DATA;
class Iterator;
// 容器的抽象基类
class Aggregate {
public:
virtual ~Aggregate(){}
virtual Iterator* CreatIterator(Aggregate *pAggregate)=0;
virtual int GetSize()=0;
virtual DATA GetItem(int nIndex)=0;
};
// 迭代器的抽象基类
class Iterator {
public:
virtual ~Iterator(){}
virtual void First()=0;
virtual void Next()=0;
virtual bool IsDone()=0;
virtual DATA CurrentItem()=0;
};
// 一个具体的容器类
class ConcreteAggregate : public Aggregate {
public:
ConcreteAggregate(int nSize);
virtual ~ConcreteAggregate();
virtual Iterator* CreatIterator(Aggregate *pAggregate);
virtual int GetSize();
virtual DATA GetItem(int nIndex);
private:
int m_nSize;
DATA *m_pData;
};
// 访问ConcreteAggregate容器的迭代器类
class ConcreteIterator : public Iterator {
public:
ConcreteIterator(Aggregate* pAggreagte);
virtual ~ConcreteIterator();
virtual void First();
virtual void Next();
virtual bool IsDone();
virtual DATA CurrentItem();
private:
Aggregate *m_pConcreteAggregate;
int m_nIndex;
};
// ConcreteAggregate
ConcreteAggregate::ConcreteAggregate(int nSize) : m_nSize(nSize), m_pData(NULL) {
m_pData = new DATA[m_nSize];
for (int i = 0; i < m_nSize; ++i) {
m_pData[i] = i;
}
}
ConcreteAggregate::~ConcreteAggregate() {
delete [] m_pData;
m_pData = NULL;
}
Iterator* ConcreteAggregate::CreatIterator(Aggregate *pAggregate) {
return new ConcreteIterator(this);
}
int ConcreteAggregate::GetSize() {
return m_nSize;
}
DATA ConcreteAggregate::GetItem(int nIndex) {
if (nIndex < m_nSize) {
return m_pData[nIndex];
}
else {
return -1;
}
}
// ConcreteIterator
ConcreteIterator::ConcreteIterator(Aggregate* pAggreagte) : m_pConcreteAggregate(pAggreagte), m_nIndex(0) {
}
ConcreteIterator::~ConcreteIterator() {
if (!m_pConcreteAggregate) delete m_pConcreteAggregate;
}
void ConcreteIterator::First() {
m_nIndex = 0;
}
void ConcreteIterator::Next() {
if (m_nIndex < m_pConcreteAggregate->GetSize()) {
++m_nIndex;
}
}
bool ConcreteIterator::IsDone() {
return m_nIndex == m_pConcreteAggregate->GetSize();
}
DATA ConcreteIterator::CurrentItem() {
return m_pConcreteAggregate->GetItem(m_nIndex);
}
int main() {
Aggregate * pAggregate = new ConcreteAggregate(10);
for (Iterator * pIterator = new ConcreteIterator(pAggregate); !pIterator->IsDone(); pIterator->Next()) {
cout << pIterator->CurrentItem() << endl;
}
return 0;
}
Iterator模式C++实现的更多相关文章
- 第3月第13天 cpp模版 Iterator模式 proactor
1.模版除了传参,还可以自动创建.而传指针只是传参而已. template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_P ...
- Java 实现迭代器(Iterator)模式
类图 /** * 自己定义集合接口, 相似java.util.Collection * 用于数据存储 * @author stone * */ public interface ICollection ...
- 设计模式之Iterator模式
STL里的iterator就是应用了iterator模式. 一.什么是迭代模式 Iterator模式也叫迭代模式,是行为模式之一,它把对容器中包含的内部对象的访问委让给外部类,使用Iterator按顺 ...
- Java设计模式(12)迭代模式(Iterator模式)
上了这么多年学,我发现一个问题,好象老师都很喜欢点名,甚至点名都成了某些老师的嗜好,一日不点名,就饭吃不香,觉睡不好似的,我就觉得很奇怪,你的课要是讲的好,同学又怎么会不来听课呢,殊不知:“误人子弟, ...
- 设计模式—迭代器Iterator模式
什么是迭代器模式? 让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构. 首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现, ...
- 设计模式(一)Iterator模式
Iterator模式用于在数据集合中按照顺序遍历集合.即迭代器模式. 下面来看一段实现了迭代器模式的示例程序. 这段程序的作用是将书(Book)放置到书架(BookShelf)中,并将书的名字按顺序显 ...
- 设计模式C++描述----20.迭代器(Iterator)模式
一. 举例说明 我们知道,在 STL 里提供 Iterator 来遍历 Vector 或者 List 数据结构. Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 ...
- 【设计模式大法】Iterator模式
Iterator模式 --一个一个遍历 在Java中的for语句中 i++的作用是让 i 的值在每次循环后自增1,这样就可以访问数组中的下一个元素.下下一个元素.再下下一个元素,也就实现了从头至尾逐一 ...
- Java设计模式之Iterator模式
分类: [java]2013-07-15 10:58 917人阅读 评论(0) 收藏 举报 所谓Iterator模式,即是Iterator为不同的容器提供一个统一的访问方式.本文以java中的容器为例 ...
- 1、迭代器 Iterator模式 一个一个遍历 行为型设计模式
1.Iterator模式 迭代器(iterator)有时又称游标(cursor)是程序设计的软件设计模式,可在容器(container,例如链表或者阵列)上遍访的接口,设计人员无需关心容器的内容. I ...
随机推荐
- window servet 2012 r2 配置php服务器环境
绑定:https://jingyan.baidu.com/article/0bc808fc2c6a851bd485b92a.html 配置环境:http://www.jb51.net/article/ ...
- The new SFCB broker fails to start with a SSL-related error: Failure setting ECDH curve name (secp22
# openssl ecparam -list_curves secp384r1 : NIST/SECG curve over a 384 bit prime field secp521r1 : NI ...
- 微软的 Sysinternals 系统管理工具包,例如可找出自动启动的流氓软件
作者:Kenny链接:https://www.zhihu.com/question/52157612/answer/153886419来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- Synchronous and Asynchronous I/O [Windows]
There are two types of input/output (I/O) synchronization: synchronous I/O and asynchronous I/O. Asy ...
- 使用Spring Boot搭建你的第一个应用程序
文章目录 依赖配置 main程序配置 MVC配置 安全配置 存储 Web 页面和Controller 异常处理 测试 结论 Spring Boot是Spring平台的约定式的应用框架,使用Spring ...
- Eclipse Mac OS 安装 Subversion插件subclipse 缺失JavaHL解决方案
安装 SVN 插件 subclipse 时可能遇到问题 subclipse 安装完成后,当我们选择使用 的时候还是会提示:javaHL not available, SVN接口选择 client:选择 ...
- java的基础知识01
来自<head first java>书籍的摘录
- The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 K题 center
You are given a point set with nn points on the 2D-plane, your task is to find the smallest number o ...
- JDK 配置环境变量
1.配置环境变量 右击 我的电脑 --> 属性 --> 高级系统设置 --> 高级 --> 环境变量 在系统变量里新建 JAVA_HOME 变量,变量值如下 D:\work_s ...
- 使用Codemirror打造Markdown编辑器
前几天突然想给自己的在线编译器加一个Markdown编辑功能,于是花了两三天敲敲打打初步实现了这个功能. 一个Markdown编辑器需要有如下常用功能: 粗体 斜体 中划线 标题 链接 图片 引用 代 ...