Iterator.H
#pragma once
#include <list>
#include <windows.h>
using namespace std;/*
设计模式-迭代器模式(Iterator)
提供一种方法顺序访问一个聚合对象中的各个元素,而不暴露该对象内部表示。
(现在好多语言都已经内置实现了这个功能了,所以实际用途不大,
但是建议写一下,实现过程价值远远大于使用价值。)
*/class Iterator{//迭代器抽象类public:virtual void * First() = 0;virtual void * Next() = 0;virtual BOOL IsDone() = 0;virtual void * CurrentItem() = 0;
};class CAggregate {//聚集抽象类public:virtual Iterator * CreateIterator() = 0;virtual void Insert(void * const pNode) = 0;virtual void Remove(void * const pNode) = 0;
};class CConcreteAggregate :public CAggregate {//具体聚集类public:list<void*>mpItems;CConcreteAggregate();void Clear();Iterator * CreateIterator();void Insert(void * const pNode);void Remove(void * const pNode);
};class CConcreteIterator : public Iterator {//具体的迭代器类private:CConcreteAggregate *mpAggregate = NULL;int mnCurrent = 0;
public:CConcreteIterator(CConcreteAggregate * pAggregate);void * First();void * Next();BOOL IsDone();void * CurrentItem();
};Iterator.CPP
#include "stdafx.h"
#include "Iterator.h"CConcreteAggregate::CConcreteAggregate() {mpItems.clear();
}void CConcreteAggregate::Clear() {for each(auto i in mpItems) {delete i;}
}Iterator * CConcreteAggregate::CreateIterator() {return new CConcreteIterator(this);
}void CConcreteAggregate::Insert(void * const pNode) {mpItems.push_back(pNode);
}
void CConcreteAggregate::Remove(void * const pNode) {mpItems.remove(pNode);
}CConcreteIterator::CConcreteIterator(CConcreteAggregate * pAggregate) {mpAggregate = pAggregate;mnCurrent = 0;
}void * CConcreteIterator::First() {return mpAggregate->mpItems.size() == 0 ? NULL :*(mpAggregate->mpItems.begin());
}
void * CConcreteIterator::Next() {if (IsDone()) return NULL;int nSubscript = 0;mnCurrent++;for each(auto i in mpAggregate->mpItems) {if (nSubscript++ == mnCurrent + 1) {return i;}}
}
BOOL CConcreteIterator::IsDone() {return mnCurrent >= mpAggregate->mpItems.size();
}
void * CConcreteIterator::CurrentItem() {int nSubscript = 0;for each(auto i in mpAggregate->mpItems) {if (nSubscript++ == mnCurrent) {return i;}}return NULL;
}#pragma once
#include "stdafx.h"
#include "Iterator.h"
#include<string>
#include<iostream>
using namespace std;int main() {CConcreteAggregate *pA = new CConcreteAggregate();pA->Insert(new string("node-1"));pA->Insert(new string("node-2"));string * pStr = new string("node-3");pA->Insert(pStr);Iterator *pIteratorA = new CConcreteIterator(pA);while (!pIteratorA->IsDone()) {cout << *((string*)pIteratorA->CurrentItem()) << endl;pIteratorA->Next();}pA->Remove(pStr);Iterator *pIteratorB = pA->CreateIterator();while (!pIteratorB->IsDone()) {cout << *((string*)pIteratorB->CurrentItem()) << endl;pIteratorB->Next();}pA->Clear();delete pIteratorA;delete pIteratorB;delete pA;delete pStr;getchar();return 0;
}

设计模式复习-迭代器模式相关推荐

  1. 【GOF23设计模式】迭代器模式

    [GOF23设计模式]迭代器模式 来源:http://www.bjsxt.com/  一.[GOF23设计模式]_迭代器模式.JDK内置迭代器.内部类迭代器 1 package com.test.it ...

  2. [转载] Python进阶:设计模式之迭代器模式

    参考链接: Python中的迭代器 在软件开发领域中,人们经常会用到这一个概念--"设计模式"(design pattern),它是一种针对软件设计的共性问题而提出的解决方案.在一 ...

  3. 每日学一个设计模式1——迭代器模式

    引言 精通设计模式是从码农脱颖而出的条件之一.跟着<图解设计模式>这本书学习设计模式,从今天开始,一天总结一个设计模式. 迭代器模式(一个一个遍历) 用处 隐藏遍历集合的内部结构,遍历不同 ...

  4. php迭代器实例,php设计模式之迭代器模式实例分析【星际争霸游戏案例】

    本文实例讲述了php设计模式之迭代器模式.分享给大家供大家参考,具体如下: 星际的任务关一般会有这样的设定:一开始电脑的农民不采矿,如果战斗打响,或者玩家造出第一个兵,电脑的农民开始采矿. 我们自然会 ...

  5. 设计模式之迭代器模式(Iterator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程,它们帮助一个系统独立于如何创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  6. php foreach 循环 判断index 小于多少_PHP设计模式之迭代器模式 - 硬核项目经理

    一说到这个模式,就不得不提循环语句.在<大话设计模式>中,作者说道这个模式现在的学习意义更大于实际意义,这是为什么呢?当然就是被foreach这货给整得.任何语言都有这种类似的语法可以方便 ...

  7. Java 设计模式之迭代器模式

    一.了解迭代器模式 1.1 什么是迭代器模式 迭代器模式提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露其内部的表示. 迭代器模式把游走的任务放在迭代器上,而不是聚合上.这样简化了聚合的接口和 ...

  8. 设计模式:迭代器模式(Iterator)

    欢迎支持笔者新作:<深入理解Kafka:核心设计与实践原理>和<RabbitMQ实战指南>,同时欢迎关注笔者的微信公众号:朱小厮的博客. 欢迎跳转到本文的原文链接:https: ...

  9. Android设计模式之——迭代器模式

    一.介绍 迭代器模式(Iterator Pattern)又称为游标(Cursor)模式,是行为型设计模式之一.迭代器模式算是一个比较古老的设计模式,其源于对容器的访问,比如Java中的List.Map ...

最新文章

  1. ThomasGDietterich_MachineLearning_personalSite
  2. transform: scale(x,y)
  3. phpbreak跳出几层循环_4.2.5 PHP break语句跳出循环
  4. osg-3D世界到屏幕
  5. 在eclipse中关于Spring和Hibernate 的XML配置如何提示类的包路径的办法
  6. linux内核make执行过程
  7. combinatorial_identities习题1.1分析与解答
  8. 清掉数据_值得收藏!面试中有哪些经典的数据库问题?
  9. vb6,sql与access 获取当前日期
  10. com.android.yf.idp,QQ轻聊版-com.tencent.qqlite_v3.3.0_apkpure.apk
  11. 【emoji大全宝典】
  12. 数据库技术-数据库概念设计
  13. python3 zip命令_Python调用zip命令正确操作方法解析
  14. 电工电子电力拖动及自动化技术考核实训台QY-DG800D
  15. Graphviz绘制链表
  16. 移动端网页特效以及常用开发框架
  17. python公园售票小程序(身份证获取简单信息+简单数据分析+简单多线程)
  18. 创建vue项目时存在的问题及解决方法
  19. 打卡leetcode第12天
  20. JS判断搜索引擎来路跳转代码---百度,360,sogou收录

热门文章

  1. 用JS的正则表达式如何判断输入框内为中文或者是英文
  2. su - oracle和su oracle有什么区别
  3. 【VM单机模拟系列】VMware P2V简单实现
  4. 艾伟:尽可能摆脱对HttpContext的依赖
  5. windows terminal 笔记
  6. python的@修饰符
  7. SpringBoot 2.0 编程方式配置,不使用默认配置方式
  8. RabbitMQ 官方NET教程(二)【工作队列】
  9. [原]unity3d ios平台内存优化(一)
  10. 让IE6、IE7、IE8支持CSS3的圆角、阴影样式