C++二十三种设计模式之迭代器模式

C++二十三种设计模式之迭代器模式

一、组成

抽象聚合类 :存储集合元素,声明管理集合元素接口。
具体聚合类 :实现管理集合元素接口。
抽象迭代器类 :声明访问和遍历聚合类元素的接口。
具体迭代器类:实现访问和遍历聚合类元素的接口。

二、特点

1、有两种定义模板的方式,template和template。建议使用template,与类定义区分明显。

2、具体聚合类使用抽象聚合类的智能指针成员变量时,需要通过this指针来调用,否则可能会出现找不到该变量的编译报错。

三、目的

不暴露集合底层实现细节的情况下访问和遍历集合中所有元素。

四、缺点

1、性能消耗问题,使用迭代器需要额外的间接层来访问元素,比直接访问元素慢。

2、类膨胀,为了支持多种遍历方式,需要定义多种具体迭代器类。

五、示例代码

javascript 复制代码
#include<iostream>
#include <vector>
#include <list>
#include <string>
#include <mutex>
#include <map>
#include<stack>

using namespace std;

template<typename T>
class Node;//节点类

template<typename T>
class AbstractLinkedList;//抽象聚合类

template<typename T>
class LinkedList;//具体聚合类

template<typename T>
class AbstractIterator;//抽象迭代器类

template<typename T>
class Iterator;//具体迭代器类

template<typename T>
class Node {
public:
	Node(T data) : m_data(data), next(nullptr) {}
	~Node() {
		cout << "~Node" << endl;
	}
	T m_data;
	shared_ptr<Node<T>> next;
};

template<typename T>
class AbstractIterator {
protected:
	AbstractIterator() {}
	virtual void Next() = 0;
	virtual bool hasNext() const = 0;
public:
	shared_ptr<Node<T>> m_current;
};

template<typename T>
class Iterator :public AbstractIterator<T> {//具体迭代器类
public:
	Iterator() {}
	Iterator(shared_ptr<Node<T>> cur) {
		this->m_current = cur;
	}
	void Next() {
		this->m_current = (this->m_current)->next;
	};
	bool hasNext() const {
		return ((this->m_current) != nullptr);
	};
};

template<typename T>
class AbstractLinkedList {
protected:
	virtual Iterator<T> Begin() = 0;
	virtual void append(T value) = 0;
	shared_ptr<Node<T>> m_head;
};

template<typename T>
class LinkedList :public AbstractLinkedList<T> {
public:
	Iterator<T> Begin() {
		return Iterator<T>(this->m_head);
	}

	void append(T value) {
		auto newNode = make_shared<Node<T>>(value);
		if (!this->m_head) {
			this->m_head = newNode;
		}
		else {
			auto cur = this->m_head;
			while (cur->next) {
				cur = cur->next;
			}
			cur->next = newNode;
		}
	}
};

int main() {
	unique_ptr<LinkedList<int>> linkedList = make_unique<LinkedList<int>>();
	linkedList->append(1);
	linkedList->append(2);
	linkedList->append(3);
	for (Iterator<int> iter = linkedList->Begin(); iter.hasNext(); iter.Next()) {
		cout << "value:" << iter.m_current->m_data << endl;
	}
}
相关推荐
乌鸦9449 分钟前
《类和对象(下)》
开发语言·c++·类和对象+
逐光沧海1 小时前
数据结构基础--蓝桥杯备考
数据结构·c++·算法·蓝桥杯
前进的程序员1 小时前
嵌入式开发中 C++ 跨平台开发经验与解决方案
开发语言·c++
菜一头包1 小时前
c++ std库中的文件操作学习笔记
c++·笔记·学习
吃个早饭3 小时前
2025年第十六届蓝桥杯大赛软件赛C/C++大学B组题解
c语言·c++·蓝桥杯
阿沁QWQ4 小时前
单例模式的两种设计
开发语言·c++·单例模式
六bring个六4 小时前
qtcreater配置opencv
c++·qt·opencv·计算机视觉·图形渲染·opengl
qwertyuiop_i4 小时前
pe文件二进制解析(用c/c++解析一个二进制pe文件)
c语言·c++·pe文件
yxc_inspire5 小时前
基于Qt的app开发第八天
开发语言·c++·qt
June`6 小时前
专题三:穷举vs暴搜vs深搜vs回溯vs剪枝(全排列)决策树与递归实现详解
c++·算法·深度优先·剪枝