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

(三)行为模式:4、迭代器模式(Iterator Pattern)(C++示例)

目录

1、迭代器模式(Iterator Pattern)含义

2、迭代器模式的UML图学习

3、迭代器模式的应用场景

4、迭代器模式的优缺点

(1)优点

(2)缺点

5、C++实现迭代器模式的实例


1、迭代器模式(Iterator Pattern)含义

迭代器模式(Iterator),提供一种方法顺序访问一个聚合对象中各个元素,而不暴露该对象的内部表示。【DP】

通过使用迭代器模式,可以将遍历算法与集合对象解耦,使得集合对象的结构和遍历算法可以独立变化。

2、迭代器模式的UML图学习

 迭代器模式的主要几个角色:

(1)迭代器(Iterator):定义了访问和遍历集合对象元素的接口,包括获取下一个元素、判断是否还有元素等方法。

(2)具体迭代器(Concrete Iterator):实现迭代器接口,对具体的集合对象进行遍历操作。

(3)集合(Aggregate):定义创建迭代器对象的接口,可以是一个抽象类或接口。

(4)具体集合(Concrete Aggregate):实现集合接口,创建相应的具体迭代器对象。

3、迭代器模式的应用场景

(1)需要遍历一个聚合对象,而又不暴露其内部表示。

(2)需要对聚合对象提供多种遍历方式。

(3)需要提供一个统一的遍历接口,以便客户端代码能够以统一的方式处理不同类型的集合对象。

4、迭代器模式的优缺点

(1)优点

        1)简化集合对象的接口:迭代器模式将遍历集合对象的责任封装到迭代器中,使得集合对象本身的接口更加简洁。

        2)支持多种遍历方式:通过定义不同的迭代器,可以支持不同的遍历方式,如正向遍历、逆向遍历等。

        3)提供了一种统一的遍历接口:迭代器模式提供了一种统一的遍历接口,使得客户端代码可以以统一的方式访问不同类型的集合对象。

(2)缺点

        1)增加了系统的复杂性:引入迭代器模式会增加额外的类和接口,增加了系统的复杂性。

        2)遍历过程中不能修改集合对象:使用迭代器遍历集合对象时,不能在遍历过程中修改集合对象,否则可能导致遍历结果不准确。

5、C++实现迭代器模式的实例


#include <iostream>
#include <vector>// 迭代器接口
class Iterator 
{
public:virtual int next() = 0;virtual bool hasNext() = 0;
};// 具体迭代器
class ConcreteIterator : public Iterator 
{
private:std::vector<int> collection;int position;public:ConcreteIterator(std::vector<int> coll) : collection(coll), position(0) {}int next() override {return collection[position++];}bool hasNext() override {return position < collection.size();}
};// 集合接口
class Aggregate 
{
public:virtual Iterator* createIterator() = 0;
};// 具体集合
class ConcreteAggregate : public Aggregate 
{
private:std::vector<int> collection;public:ConcreteAggregate(std::vector<int> coll) : collection(coll) {}Iterator* createIterator() override {return new ConcreteIterator(collection);}
};int main() 
{std::vector<int> data = {1, 2, 3, 4, 5};Aggregate* aggregate = new ConcreteAggregate(data);Iterator* iterator = aggregate->createIterator();while (iterator->hasNext()) {std::cout << iterator->next() << " ";}std::cout << std::endl;delete iterator;delete aggregate;return 0;
}

在上述示例中,我们定义了迭代器接口Iterator和具体迭代器ConcreteIterator,以及集合接口Aggregate和具体集合ConcreteAggregate。通过实现这些接口和类,我们可以创建一个包含整数元素的集合对象,并使用迭代器遍历集合中的元素。

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

相关文章:

  • React Antd form.getFieldsValue() 和 form.getFieldsValue(true) 有区别吗?
  • 浅谈Java中的观察者模式
  • C++:命名空间,缺省参数,函数重载,引用,内联函数
  • 2.Vue报错Cannot read properties of undefined (reading ‘then‘)
  • 【LeetCode 】数组简介
  • 一文解析block io生命历程
  • Python爬虫学习之旅:从入门到精通,要学多久?
  • HarmonyOS/OpenHarmony(Stage模型)卡片开发应用上下文Context使用场景一
  • MAE 论文精读 | 在CV领域自监督的Bert思想
  • C++中内存的分配
  • Qt中的垂直布局QVBoxLayout和水平布局QHBoxLayout
  • 【C#学习笔记】委托和事件
  • 堆排序简介
  • React Diff算法
  • 07 mysql5.6.x docker 启动, 无 config 目录导致客户端连接认证需要 10s
  • GO GC
  • ECharts配合Node.js爬虫实现数据可视化
  • [Linux] C获取键盘,鼠标数据
  • 户外跑步用什么耳机、户外运动耳机推荐
  • ubuntu设置系统代理
  • java定时任务如何取消
  • gitlab 9.05 版本获取合并请求的API接口报错404是为什么
  • 微服务(多级缓存)
  • 阿里云配置MySQL-server 8.0远程登录
  • 清洁能源使用的社会发展意义
  • 针对论坛系统进行功能测试和性能测试
  • Android App的设计规范
  • paddleclas ImportError: cannot import name ‘Identity‘ from ‘paddle.nn‘
  • Debezium系列之:深入理解Debezium Server Operator和实际应用Debezium Server Operator案例详解
  • ansible批量创建crontab文件并添加到定时任务