首页 > 后端开发 > C++ > 正文

C++如何实现一个命令模式_C++设计模式之请求封装与撤销/重做功能

冰火之心
发布: 2025-12-02 16:43:59
原创
946人浏览过
命令模式将请求封装为对象,支持参数化操作与撤销重做。通过Command接口、具体命令类、接收者、调用者和客户端协作,实现文本编辑器的插入删除及历史控制,便于扩展与维护。

c++如何实现一个命令模式_c++设计模式之请求封装与撤销/重做功能

在C++中实现命令模式,核心是将“请求”封装成独立对象,使得可以用不同的请求对客户进行参数化,并支持请求的撤销、重做、排队等操作。这种设计特别适用于需要支持操作历史记录的场景,比如文本编辑器、图形界面操作或游戏中的技能系统。

命令模式的基本结构

命令模式包含以下几个关键角色:

  • Command(命令接口):声明执行操作的接口,通常包含一个execute()方法,有时还包括undo()和redo()。
  • ConcreteCommand(具体命令):实现Command接口,持有对Receiver的引用,并在execute()中调用Receiver的相应方法。
  • Receiver(接收者):真正执行请求的对象,比如文档、画布或游戏角色。
  • Invoker(调用者):持有命令对象,通过调用命令的execute()来触发请求,例如按钮或菜单项。
  • Client(客户端):创建ConcreteCommand对象,并设置其接收者。

举个例子:一个简单的文本编辑器支持“插入文本”和“删除文本”操作,并能撤销上一步。

基础实现:支持执行与撤销

先定义命令基类:

立即学习C++免费学习笔记(深入)”;

class Command {
public:
    virtual ~Command() = default;
    virtual void execute() = 0;
    virtual void undo() = 0;
};
登录后复制

定义接收者——文本编辑器:

千帆AppBuilder
千帆AppBuilder

百度推出的一站式的AI原生应用开发资源和工具平台,致力于实现人人都能开发自己的AI原生应用。

千帆AppBuilder 174
查看详情 千帆AppBuilder
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中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号