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;
相关推荐
Engineer邓祥浩1 天前
设计模式学习(25) 23-23 责任链模式
学习·设计模式·责任链模式
2301_790300961 天前
嵌入式GPU编程
开发语言·c++·算法
福赖1 天前
《堆 / 栈 / 静态区区别、内存泄漏原因及排查》
c++·内存··
迷途之人不知返1 天前
类和对象(2)
c++
半桔1 天前
【设计模式】策略模式:可插拔算法,从硬编码到灵活适配,体会“算法解耦“思想
java·c++·算法·设计模式·策略模式
Engineer邓祥浩1 天前
设计模式学习(23) 23-21 状态模式
学习·设计模式·状态模式
txinyu的博客1 天前
解析muduo源码之 BoundedBlockingQueue.h
c++
楼田莉子1 天前
Linux进程间通信——System V系列
linux·服务器·c++·学习·信息与通信
321.。1 天前
从 0 到 1 实现 Linux 下的线程安全阻塞队列:基于 RAII 与条件变量
linux·开发语言·c++·学习·中间件
疯狂的喵1 天前
实时信号处理库
开发语言·c++·算法