Iterator 模式应该是最为熟悉的模式了,最简单的证明就是我在实现 Composite 模式、 Flyweight 模式、Observer 模式中就直接用到了 STL 提供的 Iterator 来遍历 Vector 或者 List 数据结构。
Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 类中进行,这样就避免了暴露这个聚合对象的内部表示的可能。
Iterator 模式中定义的对外接口可以视客户成员的便捷定义,但是基本的接口在图中的 Iterator 中已经给出了(参考 STL 的 Iterator 就知道了)。
#pragma once
//Aggregate.h
class Iterator;
typedef int Object;
class Interator;
class Aggregate
{
public:
virtual ~Aggregate();
virtual Iterator* CreateIterator() = 0;
virtual Object GetItem(int idx) = 0;
virtual int GetSize() = 0; protected:
Aggregate();
private:
};
class ConcreteAggregate:public Aggregate
{
public:
enum {SIZE = 3};
ConcreteAggregate();
~ConcreteAggregate();
Iterator* CreateIterator();
Object GetItem(int idx);
int GetSize();
protected:
private:
Object _objs[SIZE];
};
//Aggregate.cpp
#include "stdafx.h"
#include "Aggregate.h"
#include "Iterator.h"
#include <iostream>
using namespace std;
Aggregate::Aggregate()
{
}
Aggregate::~Aggregate()
{
}
ConcreteAggregate::ConcreteAggregate()
{
for (int i = 0; i < SIZE; i++)
_objs[i] = i;
}
ConcreteAggregate::~ConcreteAggregate()
{
}
Iterator* ConcreteAggregate::CreateIterator()
{
return new ConcreteIterator(this);
}
Object ConcreteAggregate::GetItem(int idx)
{
if (idx < this->GetSize())
return _objs[idx];
else
return -1;
}
int ConcreteAggregate::GetSize()
{
return SIZE;
}
#pragma once
//Iterator.h
class Aggregate;
typedef int Object;
class Iterator
{
public:
virtual ~Iterator();
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() = 0;
virtual Object CurrentItem() = 0;
protected:
Iterator();
private:
};
class ConcreteIterator:public Iterator
{
public:
ConcreteIterator(Aggregate* ag , int idx = 0);
~ConcreteIterator();
void First();
void Next();
bool IsDone();
Object CurrentItem();
protected:
private:
Aggregate* _ag;
int _idx;
};
//Iterator.cpp
#include "stdafx.h"
#include "Iterator.h"
#include "Aggregate.h"
#include <iostream>
using namespace std;
Iterator::Iterator()
{
}
Iterator::~Iterator()
{
}
ConcreteIterator::ConcreteIterator(Aggregate* ag , int idx)
{
this->_ag = ag;
this->_idx = idx;
}
ConcreteIterator::~ConcreteIterator()
{
}
Object ConcreteIterator::CurrentItem()
{
return _ag->GetItem(_idx);
}
void ConcreteIterator::First()
{
_idx = 0;
}
void ConcreteIterator::Next()
{
if (_idx < _ag->GetSize())
_idx++;
}
bool ConcreteIterator::IsDone()
{
return (_idx == _ag->GetSize());
}
int main(int argc, _TCHAR* argv[])
{
Aggregate* ag = new ConcreteAggregate();
Iterator* it = new ConcreteIterator(ag);
for (; !(it->IsDone()) ; it->Next())
{
cout<<it->CurrentItem()<<endl;
}
return 0;
}
Iterator 模式的实现代码很简单,实际上为了更好地保护 Aggregate 的状态,我们可以尽量减小Aggregate的public接口,而通过将Iterator对象声明位Aggregate的友元来给予Iterator 一些特权,获得访问 Aggregate 私有数据和方法的机会。
Iterator 模式的应用很常见,我们在开发中就经常会用到 STL 中预定义好的 Iterator 来对 STL 类进行遍历(Vector、Set 等)。