当前位置: 首页 > news >正文

C++实现设计模式---迭代器模式 (Iterator)

迭代器模式 (Iterator)

迭代器模式 是一种行为型设计模式,它提供了一种方法,顺序访问一个聚合对象中的各个元素,而又不需要暴露该对象的内部表示。


意图

  • 提供一种方法,可以顺序访问一个容器对象中的元素,而无需暴露其内部实现。
  • 将遍历行为从集合对象中分离出来,使得遍历行为可以独立变化。

使用场景

  1. 需要顺序访问一个聚合对象中的元素
    • 如数组、链表或集合。
  2. 需要支持多种遍历方式
    • 如正序遍历、反序遍历等。
  3. 需要解耦遍历算法和容器实现
    • 容器的内部结构可能经常变化,但不希望影响遍历逻辑。

参与者角色

  1. 迭代器接口 (Iterator)
    • 定义用于访问聚合对象元素的方法。
  2. 具体迭代器 (ConcreteIterator)
    • 实现迭代器接口,提供对聚合对象的具体遍历。
  3. 聚合接口 (Aggregate)
    • 定义创建迭代器的方法。
  4. 具体聚合类 (ConcreteAggregate)
    • 实现聚合接口,提供一个数据集合并创建相应的迭代器。

示例代码

以下代码展示了迭代器模式的实现,用于模拟对一个数字集合的顺序访问。

#include <iostream>
#include <vector>
#include <memory>// 迭代器接口
template <typename T>
class Iterator {
public:virtual ~Iterator() = default;// 获取下一个元素virtual T next() = 0;// 判断是否还有下一个元素virtual bool hasNext() const = 0;
};// 聚合接口
template <typename T>
class Aggregate {
public:virtual ~Aggregate() = default;// 创建迭代器virtual std::unique_ptr<Iterator<T>> createIterator() const = 0;
};// 具体迭代器
template <typename T>
class ConcreteIterator : public Iterator<T> {
private:const std::vector<T>& collection; // 引用聚合对象size_t index;                     // 当前索引public:explicit ConcreteIterator(const std::vector<T>& collection): collection(collection), index(0) {}T next() override {return collection[index++];}bool hasNext() const override {return index < collection.size();}
};// 具体聚合类
template <typename T>
class ConcreteAggregate : public Aggregate<T> {
private:std::vector<T> collection; // 存储元素的集合public:void add(const T& element) {collection.push_back(element);}std::unique_ptr<Iterator<T>> createIterator() const override {return std::make_unique<ConcreteIterator<T>>(collection);}
};// 客户端代码
int main() {// 创建一个具体聚合对象ConcreteAggregate<int> aggregate;aggregate.add(1);aggregate.add(2);aggregate.add(3);aggregate.add(4);aggregate.add(5);// 获取迭代器auto iterator = aggregate.createIterator();// 遍历聚合对象std::cout << "集合中的元素: ";while (iterator->hasNext()) {std::cout << iterator->next() << " ";}std::cout << "
";return 0;
}

代码解析

1. 迭代器接口 (Iterator)
  • 定义了 nexthasNext 方法,用于访问聚合对象中的元素。
  • 子类需要实现这些方法以提供具体的遍历逻辑。
template <typename T>
class Iterator {
public:virtual ~Iterator() = default;virtual T next() = 0;virtual bool hasNext() const = 0;
};
2. 具体迭代器 (ConcreteIterator)
  • 实现了迭代器接口。
  • 使用内部索引 index 记录当前遍历的位置,并提供 nexthasNext 方法。
template <typename T>
class ConcreteIterator : public Iterator<T> {
private:const std::vector<T>& collection;size_t index;
public:explicit ConcreteIterator(const std::vector<T>& collection): collection(collection), index(0) {}T next() override { return collection[index++]; }bool hasNext() const override { return index < collection.size(); }
};
3. 聚合接口 (Aggregate)
  • 定义了 createIterator 方法,用于创建迭代器。
template <typename T>
class Aggregate {
public:virtual ~Aggregate() = default;virtual std::unique_ptr<Iterator<T>> createIterator() const = 0;
};
4. 具体聚合类 (ConcreteAggregate)
  • 存储数据集合,并实现了 createIterator 方法,返回具体的迭代器实例。
template <typename T>
class ConcreteAggregate : public Aggregate<T> {
private:std::vector<T> collection;
public:void add(const T& element) { collection.push_back(element); }std::unique_ptr<Iterator<T>> createIterator() const override {return std::make_unique<ConcreteIterator<T>>(collection);}
};
5. 客户端代码
  • 客户端通过聚合对象获取迭代器,并通过迭代器访问集合中的元素。
int main() {ConcreteAggregate<int> aggregate;aggregate.add(1);aggregate.add(2);aggregate.add(3);auto iterator = aggregate.createIterator();while (iterator->hasNext()) {std::cout << iterator->next() << " ";}
}

优缺点

优点
  1. 统一遍历接口
    • 提供了统一的遍历接口,客户端无需了解容器的具体实现。
  2. 解耦遍历算法和容器实现
    • 容器的内部结构可以改变,而不影响遍历逻辑。
  3. 支持多种遍历方式
    • 可以为同一个容器实现多种迭代器,支持不同的遍历方式。
缺点
  1. 类数量增加
    • 每种容器需要实现对应的迭代器类。
  2. 遍历效率可能降低
    • 相比直接访问容器元素,使用迭代器可能增加额外的开销。

适用场景

  1. 需要顺序访问聚合对象中的元素
    • 如遍历集合、列表或数组。
  2. 需要支持多种遍历方式
    • 希望为容器实现不同的遍历逻辑。
  3. 希望解耦遍历算法和容器实现
    • 通过迭代器封装遍历逻辑,避免依赖容器的内部实现。

总结

迭代器模式通过将遍历逻辑封装到迭代器中,实现了聚合对象与遍历算法的解耦。它特别适用于需要顺序访问容器元素,同时希望支持多种遍历方式的场景。

http://www.lryc.cn/news/523716.html

相关文章:

  • 海康工业相机的应用部署不是简简单单!?
  • Windows电脑安装File Browser与cpolar轻松搭建本地云盘
  • mac配置 iTerm2 使用lrzsz与服务器传输文件
  • 【HBuilderX 中 Git 的使用】
  • Golang结合MySQL和DuckDB提高查询性能
  • 学技术学英语:TCP的三次握手和四次挥手
  • xiao esp32 S3播放SD卡wav音频
  • Unity中实现伤害跳字效果(简单好抄)
  • GaussDB日常维护操作
  • redis实现限流
  • 基于SpringBoot和PostGIS的各国及所属机场信息检索及可视化实现
  • python http server运行Angular 单页面路由时重定向,解决404问题
  • GPT-4o背后的语音技术
  • 微透镜阵列精准全检,白光干涉3D自动量测方案提效70%
  • Spring boot框架下的RocketMQ消息中间件
  • 记录一次 centos 启动失败
  • C++学习第五天
  • openharmony标准系统方案之瑞芯微RK3568移植案例
  • 深入理解 SSH 端口转发:本地 vs 远程 vs 动态转发
  • postman请求参数化
  • 基于 WEB 开发的汽车养护系统设计与实现
  • Nginx正向代理配置
  • 本地仓库管理之当前分支内的操作
  • 《内网穿透:网络拓展与安全防护的平衡艺术》
  • 【python写个可以运行的2048小游戏】
  • 【Flink系列】9. Flink容错机制
  • DETR论文阅读
  • 关于vite+vue3+ts项目中env.d.ts 文件详解
  • 如何优化Elasticsearch大文档查询?
  • Kotlin Bytedeco OpenCV 图像图像54 透视变换 图像矫正