【设计模式】命令模式

什么是命令模式

命令模式是一种行为型设计模式。

它可将请求转换为一个包含与请求相关的所有信息的独立对象。 该转换让你能根据不同的请求将方法参数化、 延迟请求执行或将其放入队列中, 且能实现可撤销操作。

例如你正在开发一款新的文字编辑器, 包含多个按钮的工具栏, 每个按钮对应编辑器的不同操作。 你创建了一个非常简洁的 按钮类, 可用于生成工具栏上的按钮, 还可用于生成各种对话框的通用按钮。

代码示例

命令模式通常包含以下角色:

  1. Command(命令接口):声明执行操作的接口。
  2. ConcreteCommand(具体命令):将一个接收者对象绑定于一个动作,调用接收者相应的操作,以实现Execute。
  3. Invoker(调用者):要求该命令执行这个请求。
  4. Receiver(接收者):知道如何执行一个与请求相关的操作,它执行任何具体命令。
cpp 复制代码
#include <iostream>
#include <memory>
#include <vector>

// Receiver 类
class Light {
public:
    void on() {
        std::cout << "The light is on" << std::endl;
    }

    void off() {
        std::cout << "The light is off" << std::endl;
    }
};

// Command 接口
class Command {
public:
    virtual ~Command() = default;
    virtual void execute() = 0;
};

// ConcreteCommand 类
class LightOnCommand : public Command {
public:
    explicit LightOnCommand(Light* light) : light_(light) {}

    void execute() override {
        light_->on();
    }

private:
    Light* light_;
};

class LightOffCommand : public Command {
public:
    explicit LightOffCommand(Light* light) : light_(light) {}

    void execute() override {
        light_->off();
    }

private:
    Light* light_;
};

// Invoker 类
class RemoteControl {
public:
    void setCommand(std::unique_ptr<Command> command) {
        command_ = std::move(command);
    }

    void pressButton() {
        if (command_) {
            command_->execute();
        }
    }

private:
    std::unique_ptr<Command> command_;
};

int main() {
    Light light;

    RemoteControl remote;
    remote.setCommand(std::make_unique<LightOnCommand>(&light));
    remote.pressButton(); // 输出: The light is on

    remote.setCommand(std::make_unique<LightOffCommand>(&light));
    remote.pressButton(); // 输出: The light is off

    return 0;
}

主要优点

• 解耦请求发送者和接收者:发送者和接收者之间的耦合被降低。

• 支持队列和日志记录:可以很容易地实现命令队列和日志记录功能。

• 易于扩展:新增加一个命令只需要添加一个新的命令类。

• 支持撤销操作:可以轻松实现命令的撤销和重做。

相关推荐
raindayinrain11 小时前
c++设计模式
开发语言·c++·设计模式
工一木子11 小时前
设计模式与生活⑦:你打的车,隔着一个平台
设计模式
电子科技圈12 小时前
2026 RISC-V中国峰会观察:编译调试工具链的“在场节奏”,释放了什么信号?
大数据·人工智能·设计模式·软件工程·软件构建·代码规范·risc-v
电子科技圈12 小时前
Arm电机控制开发工具哪家强?
arm开发·单片机·嵌入式硬件·设计模式·软件构建
beibeix201513 小时前
命令模式(Command Pattern)
命令模式
是2的10次方啊2 天前
设计模式与生活⑦:你打的车,隔着一个平台
设计模式
geovindu2 天前
rust: Read-Write Lock Pattern
开发语言·后端·设计模式·rust·读写锁模式·同步型模式
工一木子2 天前
设计模式与生活⑤:天冷了,给电动车套件挡风被
设计模式
是2的10次方啊3 天前
设计模式与生活⑥:问题一个个砸过来,先写张便签
设计模式
SabrinaW7113 天前
命令模式思想与 EvSim 事件调度
命令模式