Skip to content

Latest commit

 

History

History

command

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Command 命令模式(C++)

意图

将一个请求封装为一个对象,从而可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。

架构图

生活类比:遥控器按钮里装的不是电线,是一封命令信封。按下就把信封交给灯去执行;撤销则打开上一封信封做反操作。按钮不用知道灯怎么开。

flowchart LR
    classDef client fill:#C2E5FF,stroke:#007AD2,color:#1E1E1E
    classDef abs fill:#DCCCFF,stroke:#874FFF,color:#1E1E1E
    classDef concrete fill:#CDF4D3,stroke:#3E9B4B,color:#1E1E1E
    classDef extra fill:#FFE0C2,stroke:#EB7500,color:#1E1E1E
    classDef shared fill:#FFECBD,stroke:#E8A302,color:#1E1E1E
    classDef hub fill:#C6FAF6,stroke:#5AD8CC,color:#1E1E1E
    finger["手指按下"]
    remote["遥控器 调用者"]
    cmd["命令信封 execute / undo"]
    light["灯 接收者"]
    hist[(历史栈 可撤销)]
    finger ==> remote
    remote ==> cmd
    cmd -->|"execute"| light
    remote -->|"按过的信封入栈"| hist
    hist -->|"undo 拆开上一封"| cmd
    class finger,remote client
    class cmd extra
    class light concrete
    class hist shared
Loading
图中角色 本仓库示例
遥控器 RemoteControl 调用者
命令信封 LightOnCommand / LightOffCommand
灯 Light 接收者

23 张图的完整图鉴见 docs/README.md。

适用场景

  • 需要将“发出请求”与“执行请求”解耦(如遥控器按钮与具体电器操作)
  • 需要支持撤销/重做、请求排队、记录操作日志
  • 需要把请求作为参数传递、存储或在不同时间执行

实现方式

Light 是接收者,真正知道如何开关灯;Command 是抽象命令,声明 execute()/undo();LightOnCommand/LightOffCommand 把“对哪个 Light 做什么操作”封装成对象;RemoteControl(调用者)只负责触发命令并把执行过的命令压入历史栈:

void RemoteControl::press_button(std::unique_ptr<Command> command) {
    command->execute();
    history_.push_back(std::move(command));   // 记录历史,供 undo 使用
}

void RemoteControl::press_undo() {
    auto command = std::move(history_.back());
    history_.pop_back();
    command->undo();
}

RemoteControl 完全不知道 Light 的存在,只认识 Command 接口,新增一种命令(如调节亮度)不需要改动 RemoteControl。

文件说明

文件 说明
command.h 接收者 Light、抽象命令 Command、两个具体命令、调用者 RemoteControl 的声明
command.cpp 各命令与调用者的具体实现
main.cpp 依次开关客厅/卧室的灯,再连续 4 次 undo 观察历史回放
Makefile 编译与运行

编译与运行

make        # 编译
make run    # 编译并运行
make clean  # 清理

输出示例

=== 命令模式:遥控器与撤销 ===

按下按钮: 开灯命令(客厅)
  [客厅的灯] 开启
按下按钮: 开灯命令(卧室)
  [卧室的灯] 开启
按下按钮: 关灯命令(客厅)
  [客厅的灯] 关闭

按下撤销键,撤销: 关灯命令(客厅)
  [客厅的灯] 开启
按下撤销键,撤销: 开灯命令(卧室)
  [卧室的灯] 关闭
按下撤销键,撤销: 开灯命令(客厅)
  [客厅的灯] 关闭
没有可撤销的操作

要点

  1. 请求对象化 — 每个命令自己知道调用哪个接收者的哪个方法,调用者无需知道细节
  2. 撤销的对称性 — undo() 与 execute() 一一对应,是命令自身职责的一部分
  3. 调用者与接收者解耦 — RemoteControl 只依赖 Command 抽象接口,可以对接任意接收者
  4. 历史栈天然支持多级撤销 — 用 vector<unique_ptr<Command>> 记录执行顺序,逆序弹出即可逐步撤销