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;
相关推荐
勤奋的知更鸟11 分钟前
Java 编程之策略模式详解
java·设计模式·策略模式
暮乘白帝过重山28 分钟前
设计模式篇:灵活多变的策略模式
设计模式·策略模式
GodKeyNet31 分钟前
设计模式-策略模式
设计模式·策略模式
DolphinDB1 小时前
如何在C++交易系统中集成高性能回测与模拟撮合
c++
DKPT2 小时前
Java设计模式之结构型模式(外观模式)介绍与说明
java·开发语言·笔记·学习·设计模式
缘来是庄2 小时前
设计模式之外观模式
java·设计模式·外观模式
你是橙子那我是谁2 小时前
设计模式之外观模式:简化复杂系统的优雅之道
设计模式
筏.k2 小时前
C++ 网络编程(14) asio多线程模型IOThreadPool
网络·c++·架构
哈里谢顿3 小时前
python 设计模式
设计模式
爱喝茶的小茶3 小时前
周赛98补题
开发语言·c++·算法