设计模式:20 想走?可以!先买票_迭代器模式
2015-08-13 14:16
405 查看
迭代器模式:
含义:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部
何时使用:当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍历的时候,应该考虑使用迭代器模式。你需要对聚集有多种方式遍历时,可以考虑使用迭代器模式。为遍历不同的聚集结构提供如:开始、下一个、是否结束、当前哪一项等统一的接口。
迭代器角色:Iterator,负责定义访问和遍历元素的接口
具体迭代器角色:Concrete Iterator,要实现迭代器接口,并记录遍历中的当前位置
集合角色:Aggregate,负责提供创建具体迭代角色的接口
具体集合角色:Concrete Aggregate,实现创建具体迭代器角色的接口—---这个具体迭代器角色与该集合的结构相关。
实现要点:
迭代抽象:访问一个聚合对象内容而无须暴露它的内部表示
迭代多态:为便利不同集合结构提供统一接口,从而支持同样的算法在不同的集合结构上进行操作
main.cpp
Aggregate.h
ConcreteAggregate.h
Iterator.h
含义:提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部
何时使用:当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍历的时候,应该考虑使用迭代器模式。你需要对聚集有多种方式遍历时,可以考虑使用迭代器模式。为遍历不同的聚集结构提供如:开始、下一个、是否结束、当前哪一项等统一的接口。
迭代器角色:Iterator,负责定义访问和遍历元素的接口
具体迭代器角色:Concrete Iterator,要实现迭代器接口,并记录遍历中的当前位置
集合角色:Aggregate,负责提供创建具体迭代角色的接口
具体集合角色:Concrete Aggregate,实现创建具体迭代器角色的接口—---这个具体迭代器角色与该集合的结构相关。
实现要点:
迭代抽象:访问一个聚合对象内容而无须暴露它的内部表示
迭代多态:为便利不同集合结构提供统一接口,从而支持同样的算法在不同的集合结构上进行操作
main.cpp
#include <iostream> #include <stdlib.h> #include "Aggregate.h" #include "ConcreteAggregate.h" #include "Iterator.h" #include "ConcreteIterator.h" using namespace std; void process() { Aggregate<int>* aggr = new ConcreteAggregate<int>(); Iterator<int>* it = aggr->createIterator(); for(it->first() ; !it->isDone() ; it->next()) { cout << *(it->currentItem()) << endl; } delete it; delete aggr; } int main(int argc,char* argv[]) { process(); system("pause"); return 0; }
Aggregate.h
#ifndef AGGREGATE_H #define AGGREGATE_H #include "Iterator.h" template<typename Item> class Aggregate { public: Aggregate(void){} virtual ~Aggregate(void){} //由集合角色创建迭代器 virtual Iterator<Item>* createIterator() = 0; }; #endif
ConcreteAggregate.h
#ifndef CONCRETEAGGREGATE_H #define CONCRETEAGGREGATE_H #include "aggregate.h" #include <vector> //具体集合角色,用于创建迭代器接口 template<typename Item> class ConcreteAggregate : public Aggregate<Item> { public: ConcreteAggregate() { _vecData.push_back(1); _vecData.push_back(2); _vecData.push_back(3); } ~ConcreteAggregate(void) { } virtual Iterator<Item>* createIterator() { //?内存泄露 return new ConcreteIterator<Item>(this); } Item& operator[](int index) { return _vecData.at(index); } int getLen() { return _vecData.size(); } private: std::vector<Item> _vecData; }; #endif
Iterator.h
#ifndef ITERATOR_H #define ITERATOR_H //注意,有模板的尽量将声明和实现放在一个文件中 template<typename Item> class Iterator { public: Iterator(void){} virtual ~Iterator(void){} virtual void first() = 0; virtual void next() = 0; virtual Item* currentItem() = 0; virtual bool isDone() = 0; }; #endif
相关文章推荐
- 安装QEMU,虚拟mini2440开发板
- 移除input type="number"的上下箭头
- 快来寻找属于你的超级OA
- Linux深入篇之三:配置Nginx+LAMP动静分离
- 以太传输介质-RJ45网线接法(直通线、交叉线以及全反线)
- thinkphp的RBAC
- TestNG 图解说明
- 浅谈可重入函数与不可重入函数
- java 垃圾回收
- mysql left join right join区别以及优化收集
- CPU性能判断指标---上下文切换,运行队列和使用率
- Maximum Gap
- [iOS]UITableViewController 无法实现键盘自动适配
- java实现的数据库管理类(mysql)
- 【转载】简介参考《JetBrains C++ IDE 推荐》
- springMVC
- Arduino ESP8266 做Station模式连接路由器&服务器。基础模型
- UML类图中的六种关系
- 试图加载格式不正确的程序(Exception from HRESULT: 0x8007000B)
- iOS中storyboard故事板使用Segue跳转界面、传值