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;
相关推荐
橘颂TA2 分钟前
【Linux】从 “抢资源” 到 “优雅控场”:Linux 互斥锁的原理与 C++ RAII 封装实战(Ⅰ)
linux·运维·服务器·c++·算法
枫叶丹417 分钟前
【Qt开发】Qt系统(三)->事件过滤器
java·c语言·开发语言·数据库·c++·qt
坐怀不乱杯魂1 小时前
Linux - 缓存利用率
linux·c++·缓存
leiming61 小时前
c++ for_each算法
开发语言·c++·算法
_OP_CHEN1 小时前
【算法基础篇】(四十一)数论之约数问题终极攻略:从求单个约数到批量统计
c++·算法·蓝桥杯·数论·约数·算法竞赛·acm/icpc
草莓熊Lotso1 小时前
从冯诺依曼到操作系统:打通 Linux 底层核心逻辑
linux·服务器·c++·人工智能·后端·系统架构·系统安全
yuanmenghao1 小时前
自动驾驶中间件iceoryx - 内存与 Chunk 管理(一)
c++·vscode·算法·链表·中间件·自动驾驶·柔性数组
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——面试题 01.02 :判定是否互为字符串重排
c++·算法·leetcode·职场和发展·结构与算法
HABuo1 小时前
【Linux进程(二)】操作系统&Linux的进程状态深入剖析
linux·运维·服务器·c语言·c++·ubuntu·centos
糯诺诺米团1 小时前
C++多线程打包成so给JAVA后端(Ubuntu)<2>
java·开发语言·c++