疊代器
提供一種方法順序通路一個聚合對象中各個元素, 而又不需暴露該對象的内部表示.
執行個體
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 ;
}
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() = ;
virtual void Append(string name) = ;
virtual string Gain(int index) = ;
virtual Iterator* CreateIterator() = ;
};
#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() = ;
virtual string Next() = ;
virtual bool IsEnd() = ;
virtual string GetCurrent() = ;
};
#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_(){
club_ = club;
}
RunnerIterator::~RunnerIterator(){
}
string RunnerIterator::First(){
return club_->Gain();
}
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):實作相應疊代器類.
優點
- 通路一個聚合對象的内容而無需暴露它的内部表示.
- 簡化了聚合的接口.
- 在同一個聚合上可以有多個周遊.
缺點
- 對于比較簡單的周遊資料聚合,使用疊代器方式周遊較為繁瑣.