行为型模式-迭代器(iterator)
来源:互联网 发布:nginx 转发到其他ip 编辑:程序博客网 时间:2024/05/16 04:04
迭代器
提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示.
实例
main.cc:
#include <windows.h>#include "runner_club.h"#include "runner_iterator.h"#include <iostream>using namespace std;/*design_pattern:"iterator"list all members of the runner club.*/int main(){ RunnerClub *runner_club = new RunnerClub(); runner_club->Append("zhang san"); runner_club->Append("li si"); runner_club->Append("wang wu"); Iterator *iterator = runner_club->CreateIterator(); for(;!iterator->IsEnd();iterator->Next()){ cout << iterator->GetCurrent() << endl; } //clear delete runner_club; system("Pause"); return 0;}
Club:
//club.h#ifndef HELENDP_SOURCE_CLUB_H_#define HELENDP_SOURCE_CLUB_H_#include "iterator.h"#include <string>using namespace std;class Club{public: Club(); virtual ~Club(); virtual int GetTotal() = 0; virtual void Append(string name) = 0; virtual string Gain(int index) = 0; virtual Iterator* CreateIterator() = 0;};#endif//club.cc#include "club.h"Club::Club(){}Club::~Club(){}
RunnerClub:
//runner_club.h#ifndef HELENDP_SOURCE_RUNNER_CLUB_H_#define HELENDP_SOURCE_RUNNER_CLUB_H_#include "club.h"#include <vector>using namespace std;class RunnerClub : public Club{public: RunnerClub(); ~RunnerClub(); int GetTotal(); void Append(string name); string Gain(int index); Iterator* CreateIterator();private: Iterator *iterator_; vector<string> vector_;};#endif//runner_club.cc#include "runner_club.h"#include "runner_iterator.h"#include <stdio.h>#include <iostream>using namespace std;RunnerClub::RunnerClub(){ iterator_ = NULL; vector_.clear();}RunnerClub::~RunnerClub(){ if(!iterator_){ delete iterator_; iterator_ = NULL; }}int RunnerClub::GetTotal(){ return vector_.size();}void RunnerClub::Append(string name){ vector_.push_back(name);}string RunnerClub::Gain(int index){ return vector_[index % vector_.size()];}Iterator* RunnerClub::CreateIterator(){ iterator_ = new RunnerIterator(this); return iterator_;}
Iterator:
//iterator.h#ifndef HELENDP_SOURCE_ITERATOR_H_#define HELENDP_SOURCE_ITERATOR_H_#include <string>using namespace std;class Iterator{public: Iterator(); virtual ~Iterator(); virtual string First() = 0; virtual string Next() = 0; virtual bool IsEnd() = 0; virtual string GetCurrent() = 0;};#endif//iterator.cc#include "iterator.h"Iterator::Iterator(){}Iterator::~Iterator(){}
RunnerIterator:
//runner_iterator.h#ifndef HELENDP_SOURCE_RUNNER_ITERATOR_H_#define HELENDP_SOURCE_RUNNER_ITERATOR_H_#include "iterator.h"#include "club.h"class RunnerIterator : public Iterator{public: RunnerIterator(Club* club); ~RunnerIterator(); string First(); string Next(); bool IsEnd(); string GetCurrent(); private: int index_; Club *club_;};#endif//runner_iterator.cc#include "runner_iterator.h"RunnerIterator::RunnerIterator(Club* club) :index_(0){ club_ = club;}RunnerIterator::~RunnerIterator(){}string RunnerIterator::First(){ return club_->Gain(0);}string RunnerIterator::Next(){ index_ %= club_->GetTotal(); return club_->Gain(index_++);}string RunnerIterator::GetCurrent(){ return club_->Gain(index_);}bool RunnerIterator::IsEnd(){ return (index_ >= club_->GetTotal() ? true : false );}
代码和UML图(EA)工程文件,最后会整理打包上传.
UML类图
结构
- Iterator(Iterator):迭代器定义访问和遍历元素的接口.
- ConcreteIterator(RunnerIterator):实现迭代器的类.
- Aggregate(Club):创建相应迭代器的接口.
- ConcreteAggregate(RunnerClub):实现相应迭代器类.
优点
- 访问一个聚合对象的内容而无需暴露它的内部表示.
- 简化了聚合的接口.
- 在同一个聚合上可以有多个遍历.
缺点
- 对于比较简单的遍历数据聚合,使用迭代器方式遍历较为繁琐.
0 0
- Iterator迭代器(行为型模式)
- 行为型模式-迭代器(iterator)
- 行为型模式-----迭代器模式(Iterator)
- [设计模式-行为型]迭代器模式(Iterator)
- 行为型模式-Iterator ( 迭代器模式 )
- Iterator 迭代器模式 --对象行为型模式
- 十六、迭代器模式Iterator(行为型)
- 行为模式 - 迭代器(ITERATOR)
- 行为模式:Iterator(迭代器)
- 设计模式之 迭代器(Iterator)----对象行为型模式
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- 设计模式(16)-行为型-迭代器模式(Iterator)
- 行为型模式之迭代器模式(Iterator)
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- 设计模式--迭代器模式Iterator(行为型)
- 设计模式 ( 十四 ) 迭代器模式Iterator(对象行为型)
- 设计模式(行为型)之迭代器模式(Iterator Pattern)
- 设计模式(十四)迭代器模式(Iterator)-行为型
- 文章标题
- Android逆向助手反编译APK
- 干掉状态:从session到token
- 隔行变色+鼠标移上变色,for循环练习
- Tunnel Warfare HDU
- 行为型模式-迭代器(iterator)
- boke1
- 轻松搞定面试中的二叉树题目
- <Android开源库> Realm For Android~ Adapter &Supporting other library(译文)
- 使用mabatis进行mapper开发实例
- [Android]如何在不root的情况下,查看sqlite数据库
- JDBC学习笔记
- SpringMVC获取请求参数的方法
- 关于二叉树的几种遍历方法