2 Command

Davids 2025-08-10 05:10:12
Categories: > > Tags:

命令模式:

行为请求者 与 行为实现者 通常为紧耦合,将一组行为抽象为对象,实现松耦合。(撤销重做、事务)
将请求(行为)封装为对象,从而使得可以用不同的请求对客户进行参数化(对请求排队、记录请求日志、支持可撤销的操作)
通过使用 Composite 模式,可以将 多个命令 封装成一个 复合对象。
与 C++ 函数对象相似:
Command : 通过 接口-实现 来定义行为,更严格,但运行时有开销。
函数对: 通过 函数签名 来定义行为,更灵活,性能更高。

1
2
3
4
5
6
7
8
9
10
class Command {
private:
std::string m_args;
public:
Command(std::string args) : m_args(args) {}
virtual void excute() = 0;
};

class CommandA { ... }
class CommandB { ... }