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;
	}
}
相关推荐
SuperCandyXu1 分钟前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
愈努力俞幸运1 小时前
c++ 头文件
开发语言·c++
~山有木兮1 小时前
C++设计模式 - 单例模式
c++·单例模式·设计模式
十五年专注C++开发1 小时前
CMake基础:gcc/g++编译选项详解
开发语言·c++·gcc·g++
周某某~2 小时前
四.抽象工厂模式
java·设计模式·抽象工厂模式
HUN金克斯2 小时前
C++/C函数
c语言·开发语言·c++
慢半拍iii2 小时前
数据结构——F/图
c语言·开发语言·数据结构·c++
iceslime2 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
勤奋的知更鸟3 小时前
Java编程之组合模式
java·开发语言·设计模式·组合模式
哆啦A梦的口袋呀3 小时前
基于Python学习《Head First设计模式》第九章 迭代器和组合模式
python·学习·设计模式