设计模式22--Iterator模式(迭代器模式)---行为型模式

       Iterator 模式应该是最为熟悉的模式了,最简单的证明就是我在实现 Composite 模式、 Flyweight 模式、Observer 模式中就直接用到了 STL 提供的 Iterator 来遍历 Vector 或者 List 数据结构。

       Iterator 模式也正是用来解决对一个聚合对象的遍历问题,将对聚合的遍历封装到一个 类中进行,这样就避免了暴露这个聚合对象的内部表示的可能。

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

 

#pragma once
//Aggregate.h 
class Iterator; 
typedef int Object; 
class Interator; 
class Aggregate 
{ 
public: 
	virtual ~Aggregate(); 
	virtual Iterator* CreateIterator() = 0; 
	virtual Object GetItem(int idx) = 0; 
	virtual int GetSize() = 0; protected: 
	Aggregate(); 
private: 
}; 
class ConcreteAggregate:public Aggregate 
{ 
public: 
	enum {SIZE = 3}; 
	ConcreteAggregate(); 
	~ConcreteAggregate(); 
	Iterator* CreateIterator(); 
	Object GetItem(int idx); 
	int GetSize(); 
protected:
private: 
	Object _objs[SIZE];
};


//Aggregate.cpp 
#include "stdafx.h"
#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); 
} 
Object ConcreteAggregate::GetItem(int idx) 
{ 
	if (idx < this->GetSize()) 
		return _objs[idx]; 
	else
		return -1; 
} 
int ConcreteAggregate::GetSize() 
{ 
	return SIZE; 
}


#pragma once
//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:public Iterator 
{ 
public: 
	ConcreteIterator(Aggregate* ag , int idx = 0); 
	~ConcreteIterator(); 
	void First();
	void Next(); 
	bool IsDone(); 
	Object CurrentItem(); 
protected: 
private: 
	Aggregate* _ag; 
	int _idx; 
};

//Iterator.cpp 
#include "stdafx.h"
#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() 
{ 
}
Object 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()); 
}

int main(int argc, _TCHAR* argv[])
{
	Aggregate* ag = new ConcreteAggregate(); 
	Iterator* it = new ConcreteIterator(ag); 
	for (; !(it->IsDone()) ; it->Next()) 
	{ 
		cout<<it->CurrentItem()<<endl; 
	} 
	return 0;
}

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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值