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;
相关推荐
LeoLei806031 分钟前
新特性之C++17
开发语言·c++
jllws11 小时前
C++基础:STL概述
开发语言·c++
FightingLod1 小时前
C++中list容器使用详解
开发语言·c++·list
yyqzjw1 小时前
【C++】单例模式
c++·单例模式
每天的积累1 小时前
C++学习笔记二
c++·笔记·学习
zengy51 小时前
代码随想录打卡第十三天
数据结构·c++·算法·leetcode
Danica~2 小时前
RpcChannel的调用过程
网络·c++·rpc
C_player_0012 小时前
C++ list的模拟实现
c++
普通程序员A3 小时前
代码技巧专题 -- 使用策略模式编写HandleService
设计模式·面试·策略模式·代码优化·handle
逸群不凡3 小时前
C++11|lambda语法与使用
开发语言·c++