《命令模式(极简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.
*/
相关推荐
伊灵eLing14 小时前
GoLang 语言基础
开发语言·后端·golang
两年半的个人练习生^_^14 小时前
JMM 进阶:彻底理解 synchronized 实现原理
java·开发语言
小白不白11114 小时前
Invoke的用法
开发语言·人工智能·数码相机·计算机视觉·c#
techdashen14 小时前
What is maintenance, anyway?
开发语言·后端·rust
万法若空14 小时前
C/C++基本类型表示范围
c语言·开发语言·c++
yijianace14 小时前
Python爬虫实战:BooksToScrape 多线程爬取与图片下载
开发语言·爬虫·python
凡人叶枫14 小时前
Effective C++ 条款15:在资源管理类中提供对原始资源的访问
linux·开发语言·c++·stm32·单片机
swordbob14 小时前
Spring Boot 2.0 改 CGLIB 后,接口实现是否有影响
java·开发语言·spring
郝学胜-神的一滴14 小时前
中级OpenGL教程 009:用环境光告别模型死黑
前端·c++·unity·godot·图形渲染·opengl·unreal
AI人工智能+电脑小能手14 小时前
【大白话说Java面试题 第106题】【并发篇】第6题:synchronized 锁的锁对象可以是什么?
java·开发语言·面试