一、UML图
二、概念
迭代器模式(Iterator):提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示。
三、说明
什么时候用?
(1)当你需要访问一个聚集对象,而且不管这些对象时什么都需要遍历的时候,你就应该考虑用迭代器模式。
(2)你需要对聚集有多种遍历时,可以考虑用迭代器模式。
(3)为遍历不同的聚集结构提供如开始、下一个、是否结束、当前哪一项等统一的接口。
迭代器模式的好处?
迭代器模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以不暴露集合的内部结构,又可让外部代码透明地访问集合内部的数据。
四、C++实现
(1)Iterator.h
#ifndef ITERATOR_H #define ITERATOR_H #include <vector> #include <iostream> #include "Aggregate.h" typedef std::string object; //迭代器抽象类 class Iterator { public: virtual object First()=0; virtual object Next()=0; virtual bool IsDone()=0; virtual object CurrentItem()=0; }; //具体迭代器类,从前往后的迭代器 class ConcreteIterator:public Iterator { private: ConcreteAggregate* aggregate; int current; public: ConcreteIterator(Aggregate* aggregate); object First(); object Next(); bool IsDone(); object CurrentItem(); }; //具体迭代器类,从后往前的迭代器 class ConcreteIteratorDesc:public Iterator { private: ConcreteAggregate* aggregate; int current; public: ConcreteIteratorDesc(Aggregate* aggregate); object First(); object Next(); bool IsDone(); object CurrentItem(); }; #endif
(2)Iterator.cpp
#include "Iterator.h" ConcreteIterator::ConcreteIterator(Aggregate* aggregate) { this->aggregate=(ConcreteAggregate*)aggregate; current=0; } object ConcreteIterator::First() { return aggregate->GetVector()->at(0); } object ConcreteIterator::Next() { current++; if(current<aggregate->GetVector()->size()) return aggregate->GetVector()->at(current); } bool ConcreteIterator::IsDone() { return current>=aggregate->GetVector()->size()?true:false; } object ConcreteIterator::CurrentItem() { return aggregate->GetVector()->at(current); } ConcreteIteratorDesc::ConcreteIteratorDesc(Aggregate* aggregate) { this->aggregate=(ConcreteAggregate*)aggregate; current=(((ConcreteAggregate*)aggregate)->GetVector()->size())-1; } object ConcreteIteratorDesc::First() { return *(aggregate->GetVector()->end()); } object ConcreteIteratorDesc::Next() { current--; if(current>=0) return aggregate->GetVector()->at(current); } bool ConcreteIteratorDesc::IsDone() { return current<0?true:false; } object ConcreteIteratorDesc::CurrentItem() { return aggregate->GetVector()->at(current); }
(3)Aggregate.h
#ifndef AGGREGATE_H #define AGGREGATE_H #include <vector> #include <string> #include <iostream> class Iterator; class ConcreteIterator; typedef std::string object; //聚集抽象类 class Aggregate { public: virtual Iterator* CreateIterator()=0; virtual std::vector<object>* GetVector()=0; }; //具体聚集类 class ConcreteAggregate:public Aggregate { private: std::vector<object> *items; public: ConcreteAggregate(); ~ConcreteAggregate(); //产生从前往后的迭代器 Iterator* CreateIterator(); //产生从后往前的迭代器 Iterator* CreateIteratorDesc(); std::vector<object>* GetVector(); int Count(); object GetElement(int index); void SetElement(int index,object o); }; #endif
(4)Aggregate.cpp
#include "Aggregate.h" #include "Iterator.h" ConcreteAggregate::ConcreteAggregate() { items=new std::vector<object>; } ConcreteAggregate::~ConcreteAggregate() { delete items; } Iterator* ConcreteAggregate::CreateIterator() { Iterator* it=new ConcreteIterator(this); return it; } Iterator* ConcreteAggregate::CreateIteratorDesc() { Iterator* it=new ConcreteIteratorDesc(this); return it; } int ConcreteAggregate::Count() { return items->size(); } std::vector<object>* ConcreteAggregate::GetVector() { return items; } object ConcreteAggregate::GetElement(int index) { return items->at(index); } void ConcreteAggregate::SetElement(int index,object o) { items->at(index)=o; }
(5)运行截图
时间: 2024-11-18 14:47:53