迭代器模式-c++实现
来源:互联网 发布:腾讯云 centos pptp 编辑:程序博客网 时间:2024/06/11 00:18
最喜欢的一个设计模式终于实现了
// IteratorPattern.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <vector>using namespace std;template<class Iter>class Iterator{public:virtual void first() = 0;virtual void next() = 0;virtual Iter* currentItem() = 0;virtual bool isDone() = 0;virtual ~Iterator(){}};template<class Item>class ConcreteAggregate;template<class Item>class ConcreteIterator :public Iterator<Item>{private:ConcreteAggregate<Item> *aggr;int cur;public:ConcreteIterator(ConcreteAggregate<Item> *a):aggr(a),cur(0){}virtual void first(){cur = 0;}virtual void next(){if (cur < aggr->getLen())cur++;}virtual Item* currentItem(){if (cur < aggr->getLen())return &(*aggr)[cur];elsereturn NULL;}virtual bool isDone(){return (cur >= aggr->getLen());}};template<class Item>class Aggregate{public:virtual Iterator<Item>* createIterator() = 0;virtual ~Aggregate(){};};template<class Item>class ConcreteAggregate :public Aggregate<Item>{vector<Item> data;public:ConcreteAggregate(){data.push_back(1);data.push_back(2);data.push_back(3);}virtual Iterator<Item>* createIterator(){return new ConcreteIterator<Item>(this);}Item& operator[](int index){return data[index];}int getLen(){return data.size();}};int main(){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; return 0;}
0 0
- 【C++】实现Singleton模式
- 适配器模式C语言实现
- 策略模式C语言实现
- 建造模式C语言实现
- command模式(c 语言实现)
- C语言实现工厂模式
- C语言分层模式实现
- C语言分层模式实现
- <c++>c++三种单例模式实现
- 设计模式--单例模式 C++实现
- 设计模式 行为模式 命令模式 c语言 版本实现
- KMP字符串模式匹配的C实现
- Objective-C中的Observer模式实现
- Objective C 中实现单例模式
- Objective C 实现Singleton(单例)模式.
- Objective-C实现单例模式
- Objective-C中单例模式的实现
- 【Objective-C】单例模式的实现
- Android中PopupWindow的使用
- Oracle优化面试题
- CentOS正确关机方法
- git merge命令
- 性能分析之-- JAVA Thread Dump 分析综述
- 迭代器模式-c++实现
- swift利用Alamofire上传图片
- 多语言开发
- js元素获取
- Java面试100问
- Android Canvas.drawText方法中的坐标参数的正确解释
- HTML5基础与Meta http-equiv属性详解
- 用Qt绘制一张分形图
- 2016 Al-Baath University Training Camp Contest-1