不能直接std 的迭代器代替么。
Iterator:定义迭代器访问和遍历元素的接口;
ConcreteIterator:实现具体的迭代器;
Aggregate:定义的容器,创建相应迭代器对象的接口;
ConcreteAggregate:具体的容器实现创建相应迭代器的接口,该操作返回ConcreteIterator的一个适当的实例。
然后Aggregate中加入Iterator的对象这样就可以访问容器的方法了实现遍历。
实现要点:
1.迭代抽象:访问一个聚合对象的内容而无需暴露它的内部表示。
2.迭代多态:为遍历不同的集合结构提供一个统一的接口,从而支持同样的算法在不同的集合结构上进行操作。
3.迭代器的健壮性考虑:遍历的同时更改迭代器所在的集合结构,会导致问题。
适用性:
1.访问一个聚合对象的内容而无需暴露它的内部表示。
2.支持对聚合对象的多种遍历。
3.为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)。
一 简单实现:
/*
迭代器模式:提供一种方法顺序访问一个聚合对象中个各个元素,而不暴露该对像的内部表示.
模式的动机:
(1)一个聚合对象,如一个列表(List)或者一个集合(Set),应该提供一种方法来让别人可以访问
它的元素,而又不需要暴露它的内部结构。
(2)针对不同的需要,可能还要以不同的方式遍历整个聚合对象,但是我们并不希望在聚合对象的
抽象层接口中充斥着各种不同遍历的操作。
(3)怎样遍历一个聚合对象,又不需要了解聚合对象的内部结构,还能够提供多种不同的遍历方式,
这就是迭代器模式所要解决的问题。
Created by Phoenix_FuliMa
*/
#include <iostream>
#include <vector>
using namespace std;
/* object可以是任意类型的变量 */
typedef int object;
class Iterator
{
public:
virtual object begin() = 0;
virtual void next() = 0;
virtual object end() = 0;
virtual object current() = 0;
virtual bool IsDone() = 0;
};
class ConcreteAggregate
{
private:
vector<object> _objects;
public:
void AddObject(object obj)
{
_objects.push_back(obj);
}
object& operator[](int index)
{
return _objects[index];
}
int size()
{
return _objects.size();
}
};
class ConcreteIterator:public Iterator
{
public:
ConcreteAggregate *agg;
int _index;
public:
ConcreteIterator(ConcreteAggregate *agg)
{
this->agg = agg;
_index = 0;
}
virtual object begin()
{
return (*agg)[0];
}
virtual void next()
{
_index++;
}
virtual object end()
{
_index = agg->size();
return (*agg)[_index];
}
virtual object current()
{
return (*agg)[_index];
}
virtual bool IsDone()
{
return (_index == agg->size());
}
};
int main()
{
ConcreteAggregate *objects =new ConcreteAggregate();
object a = 1;
object b = 2;
object c = 3;
objects->AddObject(a);
objects->AddObject(b);
objects->AddObject(c);
ConcreteIterator *iter = new ConcreteIterator(objects);
object tmp_begin = iter->begin();
while(!iter->IsDone())
{
cout<<iter->current()<<" ";
iter->next();
}
cout<<endl;
delete objects;
delete iter;
system("pause");
return 0;
}
二,使用模版
#include"stdafx.h"
#include<string>
#include <iostream>
#include <vector>
template <typename Item>
class SongsBook;
/*** 迭代器类声明定义 ***/
template <typename Item>
class Iterator
{
public:
virtual Item* first() = 0;
virtual Item* next() = 0;
//virtual void toTop(Item* ) = 0;
virtual Item* currentItem() = 0;
virtual bool isDone() = 0;
virtual ~Iterator() {};
};
template <typename Item>
class KtvCrlor : public Iterator<Item>
{
public:
KtvCrlor(SongsBook<Item>* songsbook) : songsbook(songsbook), index(0) { }
virtual Item* first();
virtual Item* next();
virtual Item* currentItem();
virtual bool isDone();
private:
SongsBook<Item>* songsbook;
int index;
};
// --- 成员函数实现 ---
template <typename Item>
Item* KtvCrlor<Item>::first()
{
index = 0;
return (*songsbook)[0];
}
template <typename Item>
Item* KtvCrlor<Item>::next()
{
return (*songsbook)[index++];
}
template<typename Item>
Item* KtvCrlor<Item>::currentItem()
{
return (*songsbook)[index];
}
template <typename Item>
bool KtvCrlor<Item>::isDone(){
return (index >= songsbook->count());
}
/*** 聚类声明定义 ***/
template<typename Item>
class Aggregate
{
public:
Aggregate<Item>() {};
virtual Iterator<Item>* createIterator() = 0;
virtual ~Aggregate() {};
};
template<typename Item>
class SongsBook : public Aggregate<Item>
{
public:
SongsBook<Item>() : Aggregate<Item>() { };
void addSong(Item* song);
int count();
virtual Iterator<Item>* createIterator();
Item* operator[](int index);
private:
std::vector<Item*> songs;
};
// --- 成员函数实现 ---
template<typename Item>
int SongsBook<Item>::count() {
return songs.size();
}
template<typename Item>
void SongsBook<Item>::addSong(Item* song) {
if (song != NULL)
songs.push_back(song);
std::cout << "添加歌曲: " << *song << std::endl;
}
template<typename Item>
Iterator<Item>* SongsBook<Item>::createIterator()
{
return new KtvCrlor<Item>(this);
}
template<typename Item>
Item* SongsBook<Item>::operator[](int index) {
return songs[index];
}
int main()
{
SongsBook<std::string>* songsbook = new SongsBook<std::string>();
Iterator<std::string>* ktvController = songsbook->createIterator();
/* 选中歌曲 */
std::cout << "请选择歌曲" << std::endl;
songsbook->addSong(new std::string("春天里"));
songsbook->addSong(new std::string("生如夏花"));
songsbook->addSong(new std::string("我是一棵秋天的树 "));
songsbook->addSong(new std::string("冬天里的一把火"));
/* 遍历曲库 */
std::cout << std::endl;
std::cout << "当前列表中包含歌曲: " << songsbook->count() << "首" << std::endl;
while (!ktvController->isDone()) {
std::cout << *ktvController->next() << std::endl;
}
system("Pause");
}