[C++设计模式] iterator 迭代器模式
迭代器模式定义:提供一种方法顺序訪问一个聚合对象中各个元素,而又不须要暴露该对象。
迭代器分内部迭代器和外部迭代器。内部迭代器与对象耦合紧密,不推荐使用。
外部迭代器与聚合容器的内部对象松耦合,推荐使用。
迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既能够做到不暴露集合的内部结构,又可让外部代码透明地訪问集 合内部的数据。
并且,能够同一时候
定义多个迭代器来遍历。互不冲突。
对于迭代器,參考STL迭代器,仅仅须要使用详细容器的迭代器就能够遍历该容器内的聚合对象。也能够借助迭代器实现对象的诸如加入、删除和调用。
1.迭代器角色(Iterator):迭代器角色负责定义訪问和遍历元素的接口。
2.详细迭代器角色(Concrete Iterator):详细迭代器角色要实现迭代器接口,并要记录遍历中的当前位置。
3.集合角色(Aggregate):集合角色负责提供创建详细迭代器角色的接口。
4.详细集合角色(Concrete Aggregate):详细集合角色实现创建详细迭代器角色的接口——这个详细迭代器角色于该集合的结构相关。
C++迭代器模式(与STL接口保持一致)代码:
#include <iostream>
#include <vector>
using namespace std; template<class Item>
class Iterator
{
public:
virtual void first()=0;
virtual void next()=0;
virtual Item* currentItem()=0;
virtual bool isDone()=0;
virtual ~Iterator(){}
}; template<class Item>
class ConcreteAggregate; template<class Item>
class ConcreteIterator : public Iterator <Item>
{
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];
else
return 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;
}
[C++设计模式] iterator 迭代器模式的更多相关文章
- [设计模式] Iterator - 迭代器模式:由一份奥利奥早餐联想到的设计模式
Iterator - 迭代器模式 目录 前言 回顾 UML 类图 代码分析 抽象的 UML 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
- [C# 设计模式] Iterator - 迭代器模式:我与一份奥利奥早餐的故事
Iterator - 迭代器模式 目录 前言 回顾 UML 类图 代码分析 抽象的 UML 类图 思考 前言 这是一包奥利奥(数组),里面藏了很多块奥利奥饼干(数组中的元素),我将它们放在一个碟子上慢 ...
- C++设计模式-Iterator迭代器模式
ref: http://www.cnblogs.com/onlycxue/archive/2013/12/25/3490738.html
- 设计模式之迭代器模式(Iterator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)
原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...
- 设计模式学习--迭代器模式(Iterator Pattern)和组合模式(Composite Pattern)
设计模式学习--迭代器模式(Iterator Pattern) 概述 ——————————————————————————————————————————————————— 迭代器模式提供一种方法顺序 ...
- 设计模式17:Iterator 迭代器模式(行为型模式)
Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透 ...
- Python进阶:设计模式之迭代器模式
在软件开发领域中,人们经常会用到这一个概念——“设计模式”(design pattern),它是一种针对软件设计的共性问题而提出的解决方案.在一本圣经级的书籍<设计模式:可复用面向对象软件的基础 ...
- 设计模式之——迭代器模式
设计模式是开发者前辈们给我们后背的一个经验总结.有效的使用设计模式,能够帮助我们编写可复用的类.所谓"可复用",就是指将类实现为一个组件,当一个组件发生改变时,不需要对其他组件进行 ...
随机推荐
- 修改Visual Studio2010的主题颜色
第一步:打开工具->扩展管理器 第二步:搜素visual studio color theme editor 第三步:找到Visual Studio Color Theme Editor 第四步 ...
- First step in troubleshooting complex issues: Define and scope your issue properly
最近在查调试相关资料的时候,无意看到Tess的一篇关于如何快速分析复合场景问题的博文,感觉很实用,Mark备忘. My 9 questions for a pretty thorough proble ...
- Centos7 docker nginx容器搭建
一.安装docker http://www.cnblogs.com/WJ--NET/p/8553807.html 二.创建Dockerfile #创建文件夹 mkdir centos_nginx cd ...
- Python 中文注释报错解决方法
代码中一旦有了中文注释便会报错. 原因 如果文件里有非ASCII字符,需要在第一行或第二行指定编码声明. 解决方法 在第一行或是第二行加入这么一句# -- coding: utf-8 -- 完美解决
- ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBA
在MySQL 5.7版本中,备份迁移数据库的时候,还原时提示如下报错信息 ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be ...
- Find Bugs
为什么没有早点知道有这么好用的插件呢?
- sql 查询多列 小于某值
select COUNT(*) from ( select ID,H1 AS Value from Table_1 union all select ID,H2 from Table_1 union ...
- CSV文件模块的使用
---恢复内容开始--- 1.CSV模块使用流程 1.导入模块 impport CSV 2.打开文件(xxx.csv) with open('xxx.csv','a',encoding='utf-8' ...
- JS 蓝球弹起的高度 100 米 第几次高度小于1米
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Camera三维动画
一.概述 在Android中说到3D开发,我们首先想到的是OpenGL,但用起来比较复杂繁琐,不适合做应用级别的3D变换.Android为我们提供了一个简化版的3D开发入口:Camera(这里的Cam ...