命令模式将请求封装为对象,支持参数化操作与撤销重做。通过Command接口、具体命令类、接收者、调用者和客户端协作,实现文本编辑器的插入删除及历史控制,便于扩展与维护。

在C++中实现命令模式,核心是将“请求”封装成独立对象,使得可以用不同的请求对客户进行参数化,并支持请求的撤销、重做、排队等操作。这种设计特别适用于需要支持操作历史记录的场景,比如文本编辑器、图形界面操作或游戏中的技能系统。
命令模式包含以下几个关键角色:
举个例子:一个简单的文本编辑器支持“插入文本”和“删除文本”操作,并能撤销上一步。
先定义命令基类:
立即学习“C++免费学习笔记(深入)”;
class Command {
public:
virtual ~Command() = default;
virtual void execute() = 0;
virtual void undo() = 0;
};
定义接收者——文本编辑器:
class TextEditor {
std::string content;
public:
void insert(const std::string& text) {
content += text;
}
void erase(size_t len) {
if (len >= content.size()) {
content.clear();
} else {
content.erase(content.size() - len);
}
}
std::string getContent() const { return content; }
};
实现具体命令:
class InsertCommand : public Command {
TextEditor* editor;
std::string text;
public:
InsertCommand(TextEditor* e, const std::string& t) : editor(e), text(t) {}
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">void execute() override {
editor->insert(text);
}
void undo() override {
editor->erase(text.size());
}};
class DeleteCommand : public Command { TextEditor editor; std::string deletedText; public: DeleteCommand(TextEditor e, size_t len) : editor(e) { // 假设我们知道要删多少,实际中可由editor提供 deletedText = editor->getContent().substr(editor->getContent().size() - len); }
void execute() override {
editor->erase(deletedText.size());
}
void undo() override {
editor->insert(deletedText);
}};
引入一个调用者类来保存命令序列,支持撤销和重做:
class CommandHistory {
std::vector<std::unique_ptr<Command>> commands;
int current = -1; // 当前位置,用于支持redo
<p>public:
void execute(std::unique_ptr<Command> cmd) {
cmd->execute();
// 清除当前位置之后的历史(类似浏览器行为)
if (current < (int)commands.size() - 1) {
commands.erase(commands.begin() + current + 1, commands.end());
}
commands.push_back(std::move(cmd));
current++;
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">void undo() {
if (current >= 0) {
commands[current]->undo();
current--;
}
}
void redo() {
if (current < (int)commands.size() - 1) {
commands[current + 1]->execute();
current++;
}
}};
客户端代码演示如何组合这些部分:
int main() {
TextEditor editor;
CommandHistory history;
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">auto insertHello = std::make_unique<InsertCommand>(&editor, "Hello");
auto insertWorld = std::make_unique<InsertCommand>(&editor, " World");
history.execute(std::move(insertHello)); // Hello
history.execute(std::move(insertWorld)); // Hello World
std::cout << editor.getContent() << "\n"; // 输出: Hello World
history.undo(); // 撤销" World"
std::cout << editor.getContent() << "\n"; // 输出: Hello
history.redo(); // 重做
std::cout << editor.getContent() << "\n"; // 输出: Hello World
return 0;}
基本上就这些。通过命令模式,你把“动作”变成了可存储、可传递的对象。它让调用者不依赖具体操作,也方便扩展新命令而不改动现有代码。配合历史栈,轻松实现撤销/重做功能。对于更复杂的场景,可以加入命令合并(如连续输入合并为一次)、命令序列(宏)、持久化等特性。
以上就是C++如何实现一个命令模式_C++设计模式之请求封装与撤销/重做功能的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号