1. 迭代器模式(Iterator Pattern)的定义
(1)定义:提供一种方法顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。
①迭代器迭代的是具体的聚合对象(如数组和链表等),它围绕的是“访问”做文章。
②可用不同的遍历策略来遍历聚合,比如是否需要过滤
③为不同聚合结构提供统一的迭代接口,也就是说通过一个迭代接口可以访问不同的聚合结构,这叫做多态迭代。标准的迭代模式实现基本上都是支持多态迭代的。如用同一个迭代接口来实现对数组和链表的遍历。
(2)迭代器模式的结构和说明
①Iterator:迭代器接口。定义访问和遍历元素的接口。
②ConcreteInterator:具体的迭代器实现对象,会持有被迭代的具体的聚合对象的引用,并对聚合对象的遍历及跟踪遍历时的当前位置。
③Aggregate:聚合对象。提供创建相应迭代器对象的函数(如createIterator())。
④ConcreteAggregate:具体聚合对象。实现创建相应迭代器对象。
【编程实验】以统一的方式对数组和链表进行遍历(多态迭代)
//行为模式——迭代器模式 //场景:统一数组类和链表类的操作及遍历方法 //说明:1、本实例没有采用内部类来实现具体的迭代器,目的是 // 为了演示与课本相同的结构图 // 2、调用next方法,除了可以获得相应元素,而且游标下移一个。 #include <iostream> #include <string> using namespace std; //前向声明 class Iterator; //***************************辅助类*************************** //容器存储的对象 class Object { private: int id; public: Object(int id = 0) { this->id = id; } void toString() { cout << "Object:" << id << endl; } }; //链表节点 struct Node { Node* next; Object* data; Node(Object* data, Node* next) { this->next = next; this->data = data; } }; //*******************************聚合类(Aggregate)接口************************ //抽象聚合类 class Collection { public: virtual int add(Object* o) = 0; virtual int getCount() = 0; virtual Iterator* createIterator() = 0; virtual Object* getObject(int index) = 0; }; //***********************************迭代器接口******************************* //抽象迭代器 class Iterator { public: virtual Object* next() = 0; virtual bool hasNext() = 0; virtual ~Iterator(){} }; //****************************************具体迭代器******************************* //数组类的迭代器(因专为数组提供迭代,可以直接嵌入数组类作为其内部类使用, //但这里为了与课本一致,先暂时分开) class ArrayListIterator : public Iterator { private: int currentIndex; Collection* clt; public: ArrayListIterator(Collection* clt) { this ->clt = clt; currentIndex = 0; } Object* next() { return clt->getObject(currentIndex++); } bool hasNext() { bool bRet = currentIndex < clt->getCount(); return (bRet); } }; //链表类迭代器 class LinkedListIterator : public Iterator { private: Collection* clt; int currentIndex; public: LinkedListIterator(Collection* clt) { this -> clt = clt; currentIndex = 0; } Object* next() { return clt->getObject(currentIndex++); } bool hasNext() { bool bRet = currentIndex < clt->getCount(); return (bRet); } }; //************************************具体聚合类**************************************** //数组类 class ArrayList : public Collection { private: Object** objects; //对象数组; int mCount; int mSize; Iterator* mIterator; public: ArrayList() { mSize = 10; mCount = 0; objects = new Object*[mSize]; mIterator = NULL; } int getCount() { return mCount; } int add(Object* o) { if(mCount == mSize) { //扩大数组大小 mSize += 10; Object** newObjects = new Object*[mSize]; //复制 for(int i = 0; i < mSize; i++) { newObjects[i] = objects[i]; } //删除原数组,并更新objects指针 delete[] objects; objects = newObjects; } objects[mCount] = o; return mCount++; } Object* getObject(int index) { return objects[index]; } Iterator* createIterator() { if(mIterator == NULL) mIterator = new ArrayListIterator(this); return mIterator; } ~ArrayList() { delete[] objects; delete mIterator; } }; //链表类 class LinkedList : public Collection { private: Node* head; Node* tail; int mCount; Iterator* mIterator; public: LinkedList() { head = NULL; tail = NULL; mCount = 0; mIterator = NULL; } int add(Object* o) { int index = mCount; if(o != NULL) { Node* node = new Node(o, NULL); if(head == NULL) { head = node; tail = node; } tail->next = node; tail = node; ++mCount; } return index; } int getCount() { return mCount; } Iterator* createIterator() { if(mIterator == NULL) mIterator = new LinkedListIterator(this); return mIterator; } Object* getObject(int index) { int tmpIndex = 0; Object* ret = NULL; Node* node = head; while(node != NULL && tmpIndex <=index) { if(tmpIndex == index) { ret = node->data; break; } node = node->next; ++tmpIndex; } return ret; } ~LinkedList() { //清空链表 Node* node = head; Node* curr = NULL; while(node != NULL) { curr = node; delete node; node = curr->next; } delete mIterator; } }; int main() { //Collection* c = new ArrayList(); Collection* c = new LinkedList(); //面向接口编程,由于数组类和链表类继承自同一接口 //所以他们具有一的操作(如增加元素) for(int i = 0; i < 15; i++) { c->add(new Object(i)); } //由于Collection提供迭代器的接口, //所以可以该接口对Collection及子类对象进行遍历 Iterator* iter = c->createIterator(); //面向接口编程, while(iter->hasNext()) { Object* obj = iter->next(); (*obj).toString(); delete obj; } return 0; }