23种设计模式之二十二(行为模式)Iterator模式

来源:互联网 发布:不能用数据上网 编辑:程序博客网 时间:2024/05/24 00:59

一、简介

        Iterator迭代器模式为了解决的问题是:需要对一个聚合对象的遍历问题,将对聚合的遍历封装到一个类中进行,这样就避免了暴露这个聚合对象的内部表示的可能。如STL中提供的Iterator模式来遍历vector或者list的数据结构。

        Iterator模式的典型结构图为:


        Iterator 模式中定义的对外接口可以视客户成员的便捷定义,但是基本的接口在图中的Iterator中已经给出了(参考STL 的 Iterator就明白了)。

二、详解

1、代码实现

(1)代码iterator.h:

#ifndef _ITERATOR_H_#define _ITERATOR_H_class Aggregate;class Iterator {  public:    virtual ~Iterator();    virtual void First() = 0;         virtual void Next() = 0;         virtual bool IsDone() = 0;        virtual int CurrentItem() = 0;  protected:    Iterator();  private:};class ConcreteIterator:public Iterator {  public:    ConcreteIterator(Aggregate *ag, int idx = 0);        ~ConcreteIterator();         void First();        void Next();        bool IsDone();        int CurrentItem();  protected:  private:    Aggregate *_ag;        int _idx;};#endif
(2)代码iterator.cpp:
#include <iostream>#include "iterator.h"#include "aggregate.h"using namespace std;Iterator::Iterator(){}Iterator::~Iterator(){}ConcreteIterator::ConcreteIterator(Aggregate *ag, int idx){  this->_ag = ag;    this->_idx = idx;}ConcreteIterator::~ConcreteIterator(){}int 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());}
(3)代码aggregate.h:

#ifndef _AGGREGATE_H_#define _AGGREGATE_H_class Iterator;class Aggregate{  public:    virtual ~Aggregate();    virtual Iterator* CreateIterator() = 0;         virtual int GetItem(int idx) = 0;         virtual int GetSize() = 0;  protected:    Aggregate();  private:};class ConcreteAggregate : public Aggregate{  public:    enum {SIZE = 3};        ConcreteAggregate();        ~ConcreteAggregate();        Iterator *CreateIterator();         int GetItem(int idx);         int GetSize();     protected:     private:       int _objs[SIZE];};#endif
(4)代码aggregate.cpp:
#include <iostream>#include "aggregate.h"#include "iterator.h"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);}int ConcreteAggregate::GetItem(int idx){  if (idx < this->GetSize()) {      return _objs[idx];  }  else {    return -1;  }}int ConcreteAggregate::GetSize(){  return SIZE;}
(5)代码main.cpp:

#include <iostream>#include "aggregate.h"#include "iterator.h"using namespace std;int main(){    Aggregate *ag = new ConcreteAggregate();    Iterator* it = new ConcreteIterator(ag);    for (; !(it->IsDone()); it->Next()) {       cout<< "---CurrentItem:" << it->CurrentItem() <<endl;    }        delete it;    delete ag;  return 0;}
(6)makefile:
CFLAGS = -gDEFINED = #-D _VERSIONLIBS = CC = g++INCLUDES = -I./OBJS= main.o iterator.o aggregate.oTARGET= mainall:$(TARGET)$(TARGET):$(OBJS)$(CC) $(CFLAGS) -o $@ $(OBJS).SUFFIXES:.o .h.SUFFIXES:.cpp .o.cpp.o:$(CC) $(DEFINED) -c $(CFLAGS) -o $@ $<ok:./$(TARGET)clean:rm -f $(OBJS) $(TARGET) core *.log

2、运行结果

(Centos6.3系统中运行结果:)


         Iterator模式的实现代码很简单,实际上为了更好地保护Aggregate的状态,我们可以尽量减小Aggregate的public接口,而通过将Iterator对象声明位Aggregate的友元来给予Iterator一些特权,获得访问Aggregate私有数据和方法的机会。

三、总结

(1)Iterator 模式的应用很常见,在开发中就经常会用到STL中预定义好的Iterator来对STL类进行遍历(vector、set、list等)。

(2)源码已经打包上传到csdn上可登录下载(http://download.csdn.net/detail/taiyang1987912/8438483)。 

1 0
原创粉丝点击