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

【第22节】C++设计模式(行为模式)-Iterator(迭代器)模式

一、问题背景

        Iterator 模式是设计模式中最为常见和实用的模式之一。它的核心思想是将对聚合对象的遍历操作封装到一个独立的类中,从而避免暴露聚合对象的内部表示。通过 Iterator 模式,我们可以实现对聚合对象的统一遍历接口,而不需要关心聚合对象的具体实现细节。

        在实际开发中,Iterator 模式的应用非常广泛。例如,在实现 Composite 模式、Flyweight 模式、Observer 模式时,我们经常会使用 STL 提供的 Iterator 来遍历 `Vector` 或 `List` 等数据结构。Iterator 模式不仅简化了遍历操作,还提高了代码的可维护性和可扩展性。

二、模式选择

        Iterator 模式的典型结构图如下:

在 Iterator 模式中,主要包含以下几个角色:

(1)Aggregate(聚合):定义创建 Iterator 对象的接口。
(2)ConcreteAggregate(具体聚合):实现 Aggregate 接口,返回一个具体的 Iterator 实例。
(3)Iterator(迭代器):定义遍历聚合对象的接口。
(4)ConcreteIterator(具体迭代器):实现 Iterator 接口,负责遍历具体的聚合对象。

        通过这种设计,Iterator 模式将聚合对象的遍历逻辑与其内部实现分离,使得聚合对象可以独立于遍历逻辑进行修改。

三、代码实现

        下面我们将通过一个完整的 C++ 代码示例来展示如何实现 Iterator 模式。为了方便初学者的学习和参考,代码将包含详细的注释。

代码片段 1:Aggregate.h

// Aggregate.h
#ifndef _AGGREGATE_H_
#define _AGGREGATE_H_

class Iterator;
typedef int Object;  // 定义聚合对象中存储的数据类型

// Aggregate 类:定义创建 Iterator 对象的接口
class Aggregate {
public:
    virtual ~Aggregate();
    virtual Iterator* CreateIterator() = 0;  // 创建迭代器
    virtual Object GetItem(int idx) = 0;     // 获取指定位置的元素
    virtual int GetSize() = 0;               // 获取聚合对象的大小
protected:
    Aggregate();
private:
};

// ConcreteAggregate 类:具体聚合类,实现 Aggregate 接口
class ConcreteAggregate : public Aggregate {
public:
    enum { SIZE = 3 };  // 定义聚合对象的大小
    ConcreteAggregate();
    ~ConcreteAggregate();
    Iterator* CreateIterator();  // 创建迭代器
    Object GetItem(int idx);     // 获取指定位置的元素
    int GetSize();               // 获取聚合对象的大小
protected:
private:
    Object _objs[SIZE];  // 聚合对象中存储的数据
};

#endif //~_AGGREGATE_H_

代码片段 2:Aggregate.cpp

// Aggregate.cpp
#include "Aggregate.h"
#include "Iterator.h"
#include <iostream>
using namespace std;

// Aggregate 类的实现
Aggregate::Aggregate() {
    // 构造函数
}

Aggregate::~Aggregate() {
    // 析构函数
}

// ConcreteAggregate 类的实现
ConcreteAggregate::ConcreteAggregate() {
    // 初始化聚合对象中的数据
    for (int i = 0; i < SIZE; i++) {
        _objs[i] = i;
    }
}

ConcreteAggregate::~ConcreteAggregate() {
    // 析构函数
}

Iterator* ConcreteAggregate::CreateIterator() {
    // 创建具体的迭代器对象
    return new ConcreteIterator(this);
}

Object ConcreteAggregate::GetItem(int idx) {
    // 获取指定位置的元素
    if (idx < this->GetSize()) {
        return _objs[idx];
    } else {
        return -1;  // 如果索引越界,返回 -1
    }
}

int ConcreteAggregate::GetSize() {
    // 获取聚合对象的大小
    return SIZE;
}

代码片段 3:Iterator.h

// Iterator.h
#ifndef _ITERATOR_H_
#define _ITERATOR_H_

class Aggregate;
typedef int Object;  // 定义聚合对象中存储的数据类型

// Iterator 类:定义遍历聚合对象的接口
class Iterator {
public:
    virtual ~Iterator();
    virtual void First() = 0;          // 将迭代器指向第一个元素
    virtual void Next() = 0;           // 将迭代器指向下一个元素
    virtual bool IsDone() = 0;         // 判断是否遍历结束
    virtual Object CurrentItem() = 0;  // 获取当前元素
protected:
    Iterator();
private:
};

// ConcreteIterator 类:具体迭代器类,实现 Iterator 接口
class ConcreteIterator : public Iterator {
public:
    ConcreteIterator(Aggregate* ag, int idx = 0);  // 构造函数
    ~ConcreteIterator();
    void First();          // 将迭代器指向第一个元素
    void Next();           // 将迭代器指向下一个元素
    bool IsDone();         // 判断是否遍历结束
    Object CurrentItem();  // 获取当前元素
protected:
private:
    Aggregate* _ag;  // 聚合对象
    int _idx;        // 当前索引
};

#endif //~_ITERATOR_H_

代码片段 4:Iterator.cpp

// Iterator.cpp
#include "Iterator.h"
#include "Aggregate.h"
#include <iostream>
using namespace std;

// Iterator 类的实现
Iterator::Iterator() {
    // 构造函数
}

Iterator::~Iterator() {
    // 析构函数
}

// ConcreteIterator 类的实现
ConcreteIterator::ConcreteIterator(Aggregate* ag, int idx) {
    this->_ag = ag;  // 初始化聚合对象
    this->_idx = idx;  // 初始化当前索引
}

ConcreteIterator::~ConcreteIterator() {
    // 析构函数
}

Object ConcreteIterator::CurrentItem() {
    // 获取当前元素
    return _ag->GetItem(_idx);
}

void ConcreteIterator::First() {
    // 将迭代器指向第一个元素
    _idx = 0;
}

void ConcreteIterator::Next() {
    // 将迭代器指向下一个元素
    if (_idx < _ag->GetSize()) {
        _idx++;
    }
}

bool ConcreteIterator::IsDone() {
    // 判断是否遍历结束
    return (_idx == _ag->GetSize());
}

代码片段 5:main.cpp

// main.cpp
#include "Iterator.h"
#include "Aggregate.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    // 创建聚合对象
    Aggregate* ag = new ConcreteAggregate();

    // 创建迭代器对象
    Iterator* it = ag->CreateIterator();

    // 遍历聚合对象
    for (; !(it->IsDone()); it->Next()) {
        cout << it->CurrentItem() << endl;
    }

    // 释放内存
    delete it;
    delete ag;

    return 0;
}

代码说明

(1)Aggregate 类:定义了创建 Iterator 对象的接口,并提供了获取聚合对象大小和元素的方法。
(2)ConcreteAggregate 类:实现了 Aggregate 接口,内部维护了一个固定大小的数组。
(3)Iterator 类:定义了遍历聚合对象的接口,包括移动到第一个元素、移动到下一个元素、判断是否遍历结束以及获取当前元素的方法。
(4)ConcreteIterator 类:实现了 Iterator 接口,负责遍历 ConcreteAggregate 对象。

 

四、总结讨论

        Iterator 模式的应用非常广泛,尤其是在需要对聚合对象进行遍历时。通过将遍历逻辑封装到独立的 Iterator 类中,我们可以实现以下优点:

(1)简化遍历操作:客户端代码只需要调用 Iterator 的接口即可完成遍历,无需关心聚合对象的内部实现。
(2)提高代码复用性:可以为不同的聚合对象提供统一的遍历接口。
(3)增强灵活性:可以在不修改聚合对象的情况下,扩展或修改遍历逻辑。

        在实际开发中,STL 提供的 Iterator 就是一个典型的应用。例如,我们可以使用 `std::vector<int>::iterator` 来遍历 `std::vector<int>`,而无需关心 `std::vector` 的内部实现。

        Iterator 模式通过将对聚合对象的遍历操作封装到独立的类中,实现了遍历逻辑与聚合对象的解耦。这种设计模式不仅简化了代码结构,还提高了代码的可维护性和可扩展性。在实际开发中,Iterator 模式是处理聚合对象遍历问题的首选方案。

 

相关文章:

  • 关于webpack的文件打包分割,并防止js文件缓存
  • 系统设计面试总结:4、单点登录SSO的概念、优势、OAuth2.0、具体实现(含时序图和跨域登录/登出的解决方案)
  • 如何在后端服务发布过程中使用蓝绿部署
  • AI资产管理系统与ERP对接API规范 v2.3
  • 小程序中下载文件 Vue3 写法
  • Linux(Centos 7.6)命令详解:vim
  • bert模型笔记
  • vim基本操作及常用命令
  • 【PLL】分频器:其他拓扑
  • Linux 进程管理
  • 大白话html语义化标签优势与应用场景
  • git如何解除远程仓库 改变远程仓库地址
  • Elasticsearch为索引设置自动时间戳,ES自动时间戳
  • 杂项知识笔记搜集
  • 【由技及道】量子跃迁部署术:docker+jenkins+Harbor+SSH的十一维交付矩阵【人工智障AI2077的开发日志011】
  • Java算法语法学习 美丽子集的数目 - 力扣 Map接口
  • 2025软件测试面试八股文(含答案+文档)
  • 传输协议优化的博弈三角
  • Nginx 跨域配置详细讲解
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之功能优化,基础加载状态,Table8基础加载状态
  • 岳阳汨罗网站建设/磁力狗在线
  • 武汉市有做网站的吗/扬州百度关键词优化
  • 域名 去掉wordpress/网络优化的基本方法
  • 电商网站建设技术交流问题/大连seo按天付费
  • e4a做网站软件/上海seo顾问推推蛙
  • 网站连通率/bt搜索引擎下载