Iterator模式
来源:互联网 发布:python 读取zip文件 编辑:程序博客网 时间:2024/06/05 18:38
Iterator模式
一.意图
提供一种方法顺序访问一个聚合对象中各个元素,而又不需暴露该对象的内部表示。
二.适用性
1. 访问一个聚合对象的内容而无需暴露它的内部表示
2. 支持对聚合对象的多种遍历
3. 为遍历不同聚合结构提供一个统一的接口(即支持多态迭代)。
三.结构
四.代码
Iterator类
H文件
#ifndef ITERATOR_H#define ITERATOR_H//迭代器接口类class CIterator{public: virtual void First(){} virtual void Next(){} virtual bool IsDone(){} virtual void* CurrentItem(){}};#endif // ITERATOR_H
ConcreteIteator类
H文件
#ifndef CONCRETEITEATOR_H#define CONCRETEITEATOR_H#include "ConcreteAggregate.h"class CConcreteAggregate;//迭代器接口实现类class CConcreteIterator : public CIterator{public: //将容器实现类传入 CConcreteIterator(CConcreteAggregate* conAgg); //设置第一个 void First(); //判断是否最后一个 bool IsDone(); //下一个 void Next(); //获取容器index的实例 void* CurrentItem();private: CConcreteAggregate* m_concreteAggregate; int m_index = 0;};#endif // CONCRETEITEATOR_H
CPP文件
#include "ConcreteIteator.h"CConcreteIterator::CConcreteIterator(CConcreteAggregate* conAgg){ m_concreteAggregate = conAgg;}void CConcreteIterator::First(){ m_index = 0;}bool CConcreteIterator::IsDone(){ if(m_index < m_concreteAggregate->GetLength()) { return true; } else { return false; }}void CConcreteIterator::Next(){ if(m_index < m_concreteAggregate->GetLength()) { m_index++; }}void* CConcreteIterator::CurrentItem(){ if(m_index < m_concreteAggregate->GetLength()) { return m_concreteAggregate->GetIndex(m_index); } else { return NULL; }}
ConcreteAggregate类
H文件
#ifndef CONCRETEAGGREGATE_H#define CONCRETEAGGREGATE_H#include "Iterator.h"#include "ConcreteIteator.h"#include <vector>#include <string>using namespace std;//容器接口class CAggregate{public: virtual CIterator* CreateIterator(){}};//容器存放数据class CBook{public: CBook(string name) { this->m_name = name; } string GetName() { return m_name; }private: string m_name="";};//容器接口实现类class CConcreteAggregate : public CAggregate{public: CConcreteAggregate(); //根据index获取数据 CBook* GetIndex(int index); //添加数据 void AppandBook(CBook* book); //获取容器中数据个数 int GetLength(); //获取迭代器对象 virtual CIterator* CreateIterator();private: vector<CBook*> m_pBook;};#endif // CONCRETEAGGREGATE_H
CPP文件
#include "ConcreteAggregate.h"CConcreteAggregate::CConcreteAggregate(){}CBook* CConcreteAggregate::GetIndex(int index){ if(index >= m_pBook.size()) { return NULL; } else { return m_pBook[index]; }}void CConcreteAggregate::AppandBook(CBook* book){ m_pBook.push_back(book);}int CConcreteAggregate::GetLength(){ return m_pBook.size();}CIterator* CConcreteAggregate::CreateIterator(){ return new CConcreteIterator(this);}
Main文件
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <vector>#include <math.h>#include "ConcreteAggregate.h"#include "ConcreteIteator.h"using namespace std;//Iterator mode//迭代器设计模式的总体思想就是“在容器实现类中实现对实体的增删改查操作,而在迭代器实现类中实现遍历操作int main(){ cout << "Hello World!" << endl; //容器类 CConcreteAggregate* aggregate = new CConcreteAggregate(); aggregate->AppandBook(new CBook("Book 1")); aggregate->AppandBook(new CBook("Book 2")); aggregate->AppandBook(new CBook("Book 3")); aggregate->AppandBook(new CBook("Book 4")); aggregate->AppandBook(new CBook("Book 5")); //获取迭代器 CIterator* it = aggregate->CreateIterator(); for(it->First(); it->IsDone(); it->Next()) { CBook* book = (CBook*)it->CurrentItem(); cout << book->GetName() << endl; } if(it != NULL) { delete it; it = NULL; } if(aggregate != NULL) { delete aggregate; aggregate = NULL; } return 0;}
0 0
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator 模式
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator 模式
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator模式
- Iterator 模式
- Iterator模式
- Iterator模式
- Iterator模式
- nodejs小记--socketID
- C++进程间通信---自定义消息
- HTML总结
- 腾讯云主机mysql初始密码更改解决方案
- web页面长按复制文本clipboard.js 使用教程
- Iterator模式
- PIL 安装与配置
- 深入探索并发编程之内存屏障:资源控制操作
- android静态安全检测_四大组件activity
- FastReport 打印和导出 Excel、Word 等
- 10行代码完成JS-OC交互逻辑清晰简洁易懂
- 查看手机UDID的方法
- QuartzNet中的Cron表达式详解
- Spring集成Redis使用RedisTemplate