设计模式(16)-行为型模式-ITERATOR模式

来源:互联网 发布:决对争锋网络剧资源 编辑:程序博客网 时间:2024/06/16 21:32

别名:cursor

3.4.1功能

                学过STL的都知道iterator,无需多言。

ü 提供一种方法顺序访问一个聚合对象中各个元素 ,  而又不需暴露该对象的内部表示

ü 支持对聚合对象的多种遍历

ü 为遍历不同的聚合结构提供一个统一的接口(即, 支持多态迭代)

                这一模式的关键思想是将对列表的访问和遍历从列表对象中分离出来并放入一个迭代器 (it e r a t o r )对象中。迭代器类定义了一个访问该列表元素的接口。将遍历机制与列表对象分离使我们可以定义不同的迭代器来实现不同的遍历策略,而无需在列表接口中列举它们。

 

3.4.2 结 构


参与者

•  I t e r a t o r(迭代器)

— 迭代器定义访问和遍历元素的接口。

•  C o n c r e t e I t e r a t o r(具体迭代器)

— 具体迭代器实现迭代器接口。

— 对该聚合遍历时跟踪当前位置。

•  A g g r e g a t e(聚合)

— 聚合定义创建相应迭代器对象的接口。(备注:写了参与者,应该是说这个参与者的主要功能以及该参与者与其他参与者的关系。这里忽略了其主功能部分直接描述与迭代器的关系,假定大家都懂了。聚合就是Container或者叫Collection,如list等)

•  C o n c r e t e A g g r e g a t e(具体聚合)

— 具体聚合实现创建相应迭代器的接口,该操作返回C o n c r e t e I t e r a t o r的一个适当的实例。

协作关系

•ConcreteIterator 跟踪聚合中的当前对象,并能够计算出待遍历的后继对象。

3.4.3 C++代码

//Aggregate.h

#ifndef _AGGREGATE_H_

#define _AGGREGATE_H_

class Iterator;

typedef int Object;

class Interator;

class Aggregate

{

public:

       virtual ~Aggregate();

       virtual Iterator* CreateIterator() = 0;

       virtual ObjectGetItem(int idx) = 0;

       virtual int GetSize() = 0;

protected:

       Aggregate();

private:

};

 

class ConcreteAggregate :publicAggregate

{

public:

       enum { SIZE = 3 };

       ConcreteAggregate();

       ~ConcreteAggregate();

       Iterator* CreateIterator();

       Object GetItem(int idx);

       int GetSize();

protected:

private:

       Object _objs[SIZE];

};

#endif //~_AGGREGATE_H_

 

//Aggregate.cpp

#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);

}

 

ObjectConcreteAggregate::GetItem(int idx)

{

       if (idx<this->GetSize())

              return _objs[idx];

       else

              return -1;

}

 

int ConcreteAggregate::GetSize()

{

       return SIZE;

}

 

//Iterator.h

#ifndef _ITERATOR_H_

#define _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 :publicIterator

{

public:

       ConcreteIterator(Aggregate*ag,int idx =

              0);

       ~ConcreteIterator();

       void First();

       void Next();

       bool IsDone();

       Object CurrentItem();

protected:

private:

       Aggregate* _ag;

       int _idx;

};

#endif //~_ITERATOR_H_

 

//Iterator.cpp

#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()

{

}

ObjectConcreteIterator::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());

}

 

//main.cpp

#include"Iterator.h"

#include"Aggregate.h"

#include<iostream>

using namespace std;

 

int main(int argc, char*argv[])

{

       Aggregate*ag = new ConcreteAggregate();

       Iterator* it = new ConcreteIterator(ag);

       for (; !(it->IsDone()); it->Next())

       {

              cout << it->CurrentItem()<< endl;

       }

       return 0;

}

0 0
原创粉丝点击