共计 1649 个字符,预计需要花费 5 分钟才能阅读完成。
简介
迭代器模式,提供一种方法访问聚合对象中的各个元素,而不需要暴露它的内部实现。迭代器模式在 stl 的容器中被普遍使用,实际开发中自己实现一个迭代器并不多见,使用居多。
UML 类图
示例
以数组为例,来说明实现迭代器模式,使用了模板类,可以实例化多种类型的数组。
迭代器和聚合对象相关类,iterator.h
#ifndef ITERATOR_H
#define ITERATOR_H
const int nSize = 10;
template <class T> class CAggregate;
template<class T>
class CIterator
{
public:
CIterator(){m_nIndex = 0;}
virtual void First() = 0;
virtual void Next() = 0;
virtual bool HasNext() = 0;
virtual T GetCurrentItem() = 0;
protected:
int m_nIndex;
};
template<class T>
class CConcreteIterator:public CIterator<T>
{
public:
CConcreteIterator(CAggregate<T>* pAggregate)
{m_pAggregate = pAggregate;}
void First()
{m_nIndex = 0;}
void Next()
{++m_nIndex;}
bool HasNext()
{if(m_nIndex < nSize && m_nIndex >= 0)
return true;
return false;
}
T GetCurrentItem()
{return m_pAggregate->GetItem(m_nIndex);
}
private:
CAggregate<T>* m_pAggregate;
};
template <class T>
class CAggregate
{
public:
CAggregate(){m_nCurIndex = 0;}
virtual CIterator<T>* CreateIterator() = 0;
virtual T GetItem(const int nIndex) = 0;
virtual void Add(T t) = 0;
protected:
T m_arralist[nSize];
int m_nCurIndex;
};
template <class T>
class CConcreteAggregate:public CAggregate<T>
{
public:
CConcreteAggregate(){}
CIterator<T>* CreateIterator()
{return new CConcreteIterator<T>(this);
}
T GetItem(const int nIndex)
{return m_arralist[nIndex];
}
void Add(T t)
{m_arralist[m_nCurIndex] = t;
++m_nCurIndex;
}
};
#endif
客户端调用,main.cpp
#include <iostream>
#include "iterator.h"
using namespace std;
#define SAFE_DELETE(p) if(p){delete (p); (p) = NULL;}
int main(int argc, char* argv[])
{
CAggregate<int>* pAggregate = new CConcreteAggregate<int>;
for(int i = 0; i < nSize; ++ i)
{pAggregate->Add(i);
}
CIterator<int>* pIter = pAggregate->CreateIterator();
for(; pIter->HasNext() ;)
{cout<<pIter->GetCurrentItem()<<endl;
pIter->Next();}
SAFE_DELETE(pIter);
SAFE_DELETE(pAggregate);
return 0;
}
正文完