C++ 设计模式之迭代器模式

C++ 设计模式之迭代器模式

简介

1、迭代器模式(Iterator)是一种行为型设计模式,它允许我们顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示。迭代器模式提供了一种方法来遍历容器(容器对象,如列表、集合等)中的元素,而不需要了解容器底层的表示。

2、迭代器模式 (Iterator)应用场景包括但不限于:

2.1、当你的集合具有复杂的数据结构,并且你希望对客户代码隐藏其复杂性时。

2.2、当你需要一个遍历的聚合对象,而且你希望有多种不同遍历的方式时。

3、迭代器模式 (Iterator)的构成

3.1、迭代器接口(Iterator):定义访问和遍历元素的接口。

c 复制代码
template<typename T>
class Iterator
{
public:
	virtual ~Iterator() {};
	virtual bool hasNext() const = 0;
	virtual T next() = 0;
};

3.2、具体迭代器(Concrete Iterator):实现迭代器接口,并跟踪遍历具体对象中的当前位置。

c 复制代码
template<typename T>
class ConcreteAggregate
{
public:
	void add(T value);

	// 返回迭代器对象指针的嵌套类定义(具体迭代器)
	class IteratorImpl : public Iterator<T>
	{
	public:
		IteratorImpl(ConcreteAggregate& collection);
		T next();
		bool hasNext() const;

	private:
		ConcreteAggregate& collection;
		int current;
	};

	Iterator<T>* createIterator();
private:
	std::vector<T> data;
};

4、迭代器模式 (Iterator)的优点

4.1、支持不同的遍历策略:可以自定义迭代器适应不同的数据结构和遍历策略。

4.2、简化集合接口:将遍历逻辑从集合中抽离出来,集合本身的接口和实现都被简化了。

4.3、同时在不同的集合上遍历:一个集合可以有多个迭代器同时在不同位置进行遍历。

4.4、同一抽象的多个实现:可以为不同的集合结构提供一个共同的迭代器接口。

5、迭代器模式 (Iterator)的缺点

5.1、可能不必要:对于一些简单的集合操作,使用迭代器可能看起来是"过度设计"。

5.2、性能问题:创建额外的对象和调用方法可能会影响遍历的性能。

简单示例

1、定义

c 复制代码
// 迭代器接口
template<typename T>
class Iterator
{
public:
	virtual ~Iterator() {};
	virtual bool hasNext() const = 0;
	virtual T next() = 0;
};

// 具体集合
template<typename T>
class ConcreteAggregate
{
public:
	void add(T value);

	// 返回迭代器对象指针的嵌套类定义(具体迭代器)
	class IteratorImpl : public Iterator<T>
	{
	public:
		IteratorImpl(ConcreteAggregate& collection);
		T next();
		bool hasNext() const;

	private:
		ConcreteAggregate& collection;
		int current;
	};

	Iterator<T>* createIterator();
private:
	std::vector<T> data;
};

2、实现

c 复制代码
template class ConcreteAggregate<int>; // 显示实例化模板,装载其他类型会编译报错,实际项目应将模板函数定义和实现放一起

template<typename T>
void ConcreteAggregate<T>::add(T value)
{
	data.push_back(value);
}

template<typename T>
Iterator<T>* ConcreteAggregate<T>::createIterator()
{
	return new IteratorImpl(*this);
}

template<typename T>
ConcreteAggregate<T>::IteratorImpl::IteratorImpl(ConcreteAggregate& collection) : collection(collection), current(0)
{

}

template<typename T>
T ConcreteAggregate<T>::IteratorImpl::next()
{
	if (!hasNext())
	{
		throw std::out_of_range("Iterator out of range");
	}
	return collection.data[current++];
}

template<typename T>
bool ConcreteAggregate<T>::IteratorImpl::hasNext() const
{
	return current < collection.data.size();
}

3、调用

c 复制代码
ConcreteAggregate<int> collection;
collection.add(1);
collection.add(2);
collection.add(3);
collection.add(4);
auto it = collection.createIterator();
while (it->hasNext())
{
	std::cout << it->next() << std::endl;
}
delete it;
相关推荐
进击的荆棘1 分钟前
优选算法——队列+宽搜
数据结构·c++·算法·leetcode·bfs·队列
Irissgwe2 分钟前
STL简介
c++·stl
江屿风5 分钟前
C++OJ题经验总结(竞赛)4
开发语言·c++·笔记·算法·dp·双指针
码上有光5 分钟前
c++: 继承(下)
android·java·c++·多继承·菱形继承·虚继承
进击的荆棘11 分钟前
优选算法——栈
数据结构·c++·算法·leetcode·
SWAGGY..14 分钟前
【C++初阶】:(11)list的功能介绍&&list迭代器模拟实现
开发语言·c++
Irissgwe18 分钟前
二、信号与槽
c++·qt·信号与槽
不会C语言的男孩1 小时前
C++ Primer 第3章:字符串、向量和数组
开发语言·c++
code monkey.2 小时前
【Linux之旅】Linux 应用层自定义协议与序列化:从粘包问题到网络计算器
linux·网络·c++
草莓熊Lotso2 小时前
【Linux网络】深入理解 HTTP 协议(二):从协议格式到手写工业级 HTTP 服务器
linux·运维·服务器·网络·c++·http