23种设计模式 - 迭代器模式

模式定义

迭代器模式(Iterator Pattern)是一种行为型设计模式,用于顺序访问聚合对象(如集合)的元素,而无需暴露其内部结构。该模式将遍历逻辑封装在独立的迭代器对象中,使客户端能够统一处理不同类型的集合(如数组、链表、树)。


模式结构

抽象迭代器(Iterator)

  • 定义遍历接口(如hasNext()next()),声明访问元素的方法。
    具体迭代器(Concrete Iterator)
  • 实现迭代器接口,管理遍历的当前位置(如数组索引、链表节点指针)。
    抽象聚合类(Aggregate)
  • 声明创建迭代器的接口(如createIterator())。
    具体聚合类(Concrete Aggregate)
  • 存储数据集合(如数控系统的G代码指令列表),并返回对应的迭代器。

适用场景

数控系统指令遍历:按顺序解析并执行G代码指令(如G00G01)。

复杂数据结构遍历:如树形结构、图结构中的节点访问。

统一集合访问接口:隐藏不同集合(数组、链表)的内部差异。


C++示例(数控系统G代码指令遍历)

场景说明:

设计一个迭代器,遍历数控系统中的G代码指令集合(如G00 X100 Y200G01 X200 Y300),并依次执行。

cpp 复制代码
#include 
#include 
#include 

// G代码指令类
class GCodeCommand {
public:
    std::string type;  // 指令类型(如G00)
    float x, y;        // 坐标参数
    GCodeCommand(const std::string& t, float xVal, float yVal) 
        : type(t), x(xVal), y(yVal) {}
};

// 抽象迭代器
class Iterator {
public:
    virtual bool hasNext() = 0;
    virtual GCodeCommand next() = 0;
    virtual ~Iterator() = default;
};

// 具体迭代器:G代码指令迭代器
class GCodeIterator : public Iterator {
private:
    std::vector& commands;
    size_t currentPos;
public:
    GCodeIterator(std::vector& cmds) 
        : commands(cmds), currentPos(0) {}
    
    bool hasNext() override {
        return currentPos < commands.size();
    }
    
    GCodeCommand next() override {
        return commands[currentPos++];
    }
};

// 抽象聚合类
class GCodeCollection {
public:
    virtual Iterator* createIterator() = 0;
    virtual ~GCodeCollection() = default;
};

// 具体聚合类:存储G代码指令
class ConcreteGCodeCollection : public GCodeCollection {
private:
    std::vector commands;
public:
    void addCommand(const GCodeCommand& cmd) {
        commands.push_back(cmd);
    }
    
    Iterator* createIterator() override {
        return new GCodeIterator(commands);
    }
};

// 客户端代码
int main() {
    ConcreteGCodeCollection gcodeList;
    gcodeList.addCommand(GCodeCommand("G00", 100, 200));  // 快速定位
    gcodeList.addCommand(GCodeCommand("G01", 200, 300));  // 直线插补

    Iterator* it = gcodeList.createIterator();
    while (it->hasNext()) {
        GCodeCommand cmd = it->next();
        std::cout << "执行指令: " << cmd.type 
                  << " X" << cmd.x << " Y" << cmd.y << std::endl;
        // 实际数控系统会调用机床控制接口执行指令
    }
    
    delete it;
    return 0;
}

代码解析

GCodeCommand:表示一条G代码指令,包含类型和坐标参数。

GCodeIterator:遍历指令集合的具体迭代器,通过hasNext()next()逐步访问元素。

ConcreteGCodeCollection:存储G代码指令的集合,返回对应的迭代器对象。

客户端:通过迭代器依次执行所有指令,无需关心集合内部实现(如vector或链表)。


优点与局限性

优点:

  • 解耦遍历逻辑与集合结构,支持多种遍历方式。
  • 符合单一职责原则,集合类只需管理数据,迭代器处理遍历。
    局限性:
  • 增加小型集合的复杂度,可能影响性能。
  • 需要为每种集合类型实现专用迭代器。

数控系统中的应用

在数控系统中,迭代器模式可用于:

指令队列处理:按顺序执行G代码、M代码指令。

路径规划:遍历加工路径中的坐标点序列,生成控制信号。

日志回放:遍历历史操作记录,用于调试或复现加工过程。

相关推荐
ox00802 小时前
C++ 设计模式-策略模式
c++·设计模式·策略模式
工一木子4 小时前
【HeadFirst系列之HeadFirst设计模式】第8天之适配器模式与外观模式:让不兼容的接口和谐共处!
设计模式·适配器模式·外观模式
追光少年332214 小时前
迭代器模式
java·迭代器模式
付聪121015 小时前
装饰器模式
设计模式
扣丁梦想家15 小时前
设计模式教程:外观模式(Facade Pattern)
设计模式·外观模式
強云15 小时前
23种设计模式 - 装饰器模式
c++·设计模式·装饰器模式
強云15 小时前
23种设计模式 - 外观模式
设计模式·外观模式
鄃鳕16 小时前
单例模式【C++设计模式】
c++·单例模式·设计模式
扣丁梦想家19 小时前
设计模式教程:命令模式(Command Pattern)
设计模式·命令模式