命令模式通过封装请求为对象实现调用者与接收者解耦,支持撤销、重做和任务队列;其核心角色包括Command接口、ConcreteCommand、Receiver、Invoker和Client;示例中以TextEditor为接收者,OpenCommand和SaveCommand为具体命令,Button为调用者,通过execute()执行操作;扩展undo()方法可实现撤销功能,CommandHistory记录命令历史;C++11后可用std::function和lambda简化实现;命令应保持独立无状态以避免副作用。

命令模式是一种行为设计模式,它将请求封装为对象,从而使你可以用不同的请求、队列、日志来参数化其他对象。在C++中,命令模式通过抽象命令接口、具体命令类和接收者(Receiver)协作实现解耦。这种模式常用于实现撤销/重做、任务队列、远程调用等场景。
命令模式通常包含以下几个角色:
以下是一个简单的文本编辑器中“打开文件”和“保存文件”命令的实现:
#include <iostream>
#include <memory>
#include <vector>
// 接收者:执行实际操作
class TextEditor {
public:
void open() {
std::cout << "TextEditor: 打开文件\n";
}
void save() {
std::cout << "TextEditor: 保存文件\n";
}
};
// 命令接口
class Command {
public:
virtual ~Command() = default;
virtual void execute() = 0;
};
// 具体命令:打开文件
class OpenCommand : public Command {
private:
TextEditor& editor;
public:
OpenCommand(TextEditor& e) : editor(e) {}
void execute() override {
editor.open();
}
};
// 具体命令:保存文件
class SaveCommand : public Command {
private:
TextEditor& editor;
public:
SaveCommand(TextEditor& e) : editor(e) {}
void execute() override {
editor.save();
}
};
// 调用者:按钮或快捷键
class Button {
private:
std::unique_ptr<Command> command;
public:
void setCommand(std::unique_ptr<Command> cmd) {
command = std::move(cmd);
}
void click() {
if (command) {
command->execute();
}
}
};使用方式如下:
立即学习“C++免费学习笔记(深入)”;
int main() {
TextEditor editor;
// 客户端创建命令并绑定接收者
auto openCmd = std::make_unique<OpenCommand>(editor);
auto saveCmd = std::make_unique<SaveCommand>(editor);
// 设置到调用者(如按钮)
Button openButton, saveButton;
openButton.setCommand(std::move(openCmd));
saveButton.setCommand(std::move(saveCmd));
// 模拟用户点击
openButton.click(); // 输出:TextEditor: 打开文件
saveButton.click(); // 输出:TextEditor: 保存文件
return 0;
}命令模式天然适合实现撤销功能。只需在命令接口中增加undo()方法:
class Command {
public:
virtual ~Command() = default;
virtual void execute() = 0;
virtual void undo() {} // 可选,默认为空
};
class SaveCommand : public Command {
private:
TextEditor& editor;
public:
SaveCommand(TextEditor& e) : editor(e) {}
void execute() override {
editor.save();
}
void undo() override {
std::cout << "TextEditor: 撤销保存\n";
}
};调用者可以记录历史命令以支持撤销:
class CommandHistory {
private:
std::vector<std::unique_ptr<Command>> history;
public:
void push(std::unique_ptr<Command> cmd) {
history.push_back(std::move(cmd));
}
void undo() {
if (!history.empty()) {
history.back()->undo();
history.pop_back();
}
}
};C++11以后,可以用std::function和lambda表达式简化命令模式:
#include <functional>
class FunctionalCommand : public Command {
private:
std::function<void()> doAction;
std::function<void()> undoAction;
public:
FunctionalCommand(std::function<void()> exec, std::function<void()> un = []{})
: doAction(exec), undoAction(un) {}
void execute() override {
doAction();
}
void undo() override {
undoAction();
}
};
// 使用示例
TextEditor editor;
auto cmd = std::make_unique<FunctionalCommand>(
[&editor]() { editor.save(); },
[]() { std::cout << "撤销保存\n"; }
);基本上就这些。命令模式通过将操作封装成对象,实现了调用者与接收者的解耦,增强了系统的可扩展性和灵活性。不复杂但容易忽略的是,每个命令应尽量保持独立和无状态,避免共享数据带来的副作用。
以上就是C++如何实现命令模式(Command Pattern)_C++设计模式与命令模式实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号