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;
	}
}
相关推荐
ChoSeitaku1 小时前
NO.82十六届蓝桥杯备战|动态规划-从记忆化搜索到动态规划|下楼梯|数字三角形(C++)
c++·蓝桥杯·动态规划
ChoSeitaku1 小时前
NO.73十六届蓝桥杯备战|搜索算法-剪枝与优化-记忆化搜索|数的划分|小猫爬山|斐波那契数|Function|天下第一|滑雪(C++)
c++·蓝桥杯·剪枝
龙虾在剥我的壳2 小时前
OpenCV——图像融合
c++·opencv·计算机视觉
末央&2 小时前
【C++】list底层封装和实现
c++·windows·list
Zhichao_972 小时前
【UE5 C++课程系列笔记】35——HTTP基础——HTTP客户端异步请求API接口并解析响应的JSON
c++·ue5
阿猿收手吧!3 小时前
【QT】QPixmap QImage QBitmap QPicture
开发语言·c++·qt
攻城狮7号3 小时前
【第39节】windows编程:打造MFC版本任务管理器
c++·windows·mfc·任务管理器
诺亚凹凸曼3 小时前
23种设计模式-行为型模式-模板方法
设计模式
落笔映浮华丶4 小时前
C++(进阶) 第11智能指针
开发语言·c++
ephemerals__4 小时前
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
开发语言·c++