《命令模式(极简c++)》

本文章属于专栏- 概述 - 《设计模式(极简c++版)》-CSDN博客


模式说明

  • 方案: 命令模式将请求封装成一个对象,从而允许客户端参数化对象,将请求排入队列或者记录请求日志,以及支持可撤销的操作。
  • 优点:
    • 解耦了发送者和接收者,发送者无需知道接收者的细节,只需知道命令即可。
    • 支持撤销操作,因为命令对象包含了执行操作所需的信息。
  • 缺点:
    • 可能会导致类的数量增加,因为每个命令都需要一个具体的命令类。

本质思想:把调用的语义封装在一个具体的命令对象中,再创建一个执行调用命令的方法,这样对于所有传入的命令,都可以复用"调用命令的方法"。比如"一只鸟飞起来,然后吃东西",会有一个有一个调用命令的方法,做的事情是"先执行A,再执行B",然后传入命令A为"燕子飞起来",传入"燕子吃东西"。核心收益是,命令调用者的逻辑就可以复用了。

实践建议:在命令调用顺序固定,且使用高频时,把这部分逻辑抽象出命令模式。降低命令流程和命令本身的耦合。注意这块不适合放在业务占用cpu极高的代码部分,它对性能有少量影响。

代码示例:

cpp 复制代码
#include <iostream>
#include <vector>

// Command Interface
class Command {
public:
    virtual void execute() = 0;
};

// Receiver
class Bird {
public:
    virtual void fly() = 0;
    virtual void eat() = 0;
};

// Concrete Receiver
class Sparrow : public Bird {
public:
    void fly() override {
        std::cout << "Sparrow is flying." << std::endl;
    }
    void eat() override {
        std::cout << "Sparrow is eating." << std::endl;
    }
};

// Concrete Command for flying
class FlyCommand : public Command {
private:
    Bird* bird;

public:
    FlyCommand(Bird* bird) : bird(bird) {}

    void execute() override {
        bird->fly();
    }
};

// Concrete Command for eating
class EatCommand : public Command {
private:
    Bird* bird;

public:
    EatCommand(Bird* bird) : bird(bird) {}

    void execute() override {
        bird->eat();
    }
};

// Invoker
class Invoker {
private:
    std::vector<Command*> commands;

public:
    void addCommand(Command* command) {
        commands.push_back(command);
    }

    void executeCommands() {
        for (Command* command : commands) {
            command->execute();
        }
    }
};

int main() {
    Sparrow sparrow;
    FlyCommand flyCommand(&sparrow);
    EatCommand eatCommand(&sparrow);

    Invoker invoker;
    invoker.addCommand(&flyCommand);
    invoker.addCommand(&eatCommand);

    invoker.executeCommands();

    return 0;
}
/*
Output:
Sparrow is flying.
Sparrow is eating.
*/
相关推荐
阿蒙Amon几秒前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
尘浮72824 分钟前
60天python训练计划----day59
开发语言·python
Chef_Chen1 小时前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
不学会Ⅳ1 小时前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos
2401_881244401 小时前
牛客周赛99
c++
好开心啊没烦恼2 小时前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20203 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
山登绝顶我为峰 3(^v^)34 小时前
如何录制带备注的演示文稿(LaTex Beamer + Pympress)
c++·线性代数·算法·计算机·密码学·音视频·latex
Python×CATIA工业智造6 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
十五年专注C++开发7 小时前
CMake基础:条件判断详解
c++·跨平台·cmake·自动化编译