C++如何实现一个可撤销命令_C++命令模式与备忘录模式的结合应用

通过命令模式封装操作并结合备忘录模式保存状态,实现可撤销的命令。具体步骤为:1. 定义命令接口,包含execute和undo方法;2. 命令执行前保存接收者状态到备忘录;3. 执行时记录命令到历史栈;4. 撤销时调用栈顶命令的undo方法并恢复状态;5. 清理资源防止内存泄漏。该方式适用于文本编辑器等需多级撤销的应用,结构清晰且扩展性强。

在C++中实现可撤销的命令,可以通过结合命令模式(Command Pattern)和备忘录模式(Memento Pattern)来完成。命令模式负责封装操作,使其可以像对象一样被传递、存储和执行;而备忘录模式则用于保存对象的状态,以便后续恢复,从而支持撤销操作。

命令模式:封装操作为对象

命令模式将每个操作封装成一个独立的对象,包含执行(execute)和撤销(undo)方法。这样,我们可以把命令放入历史栈中,在需要时调用 undo 恢复之前的状态。

例如,假设我们有一个文本编辑器,支持插入文本的操作:

class TextEditor {
public:
    void insert(const std::string& text) {
        content += text;
    }
    void deleteLast(int length) {
        if (length <= content.size()) {
            content.erase(content.size() - length);
        }
    }
    std::string getState() const { return content; }
    void setState(const std::string& state) { content = state; }

private: std::string content; };

接下来定义命令接口:

class Command {
public:
    virtual ~Command() = default;
    virtual void execute() = 0;
    virtual void undo() = 0;
};

备忘录模式:保存和恢复状态

备忘录模式通过一个“备忘录”对象保存原发器(如 TextEditor)的内部状态,并提供有限访问权限,以维护封装性。

为 TextEditor 添加创建和恢复状态的能力:

class Memento {
public:
    Memento(const std::string& state) : state_(state) {}
    std::string getState() const { return state_; }
private:
    std::string state_;
};

// 在 TextEditor 中添加: Memento createMemento() const { return new Memento(content); } void restoreFromMemento(Memento m) { content = m->getState(); delete m; // 可选:也可由外部管理生命周期 }

结合使用:支持撤销的命令实现

现在定义具体的可撤销命令。在执行前保存当前状态,undo 时恢复该状态。

class InsertCommand : public Command {
public:
    InsertCommand(TextEditor* editor, const std::string& text)
        : editor_(editor), text_(text) {}
void execute() override {
    memento_ = editor_->createMemento(); // 执行前保存状态
    editor_->insert(text_);
}

void undo() override {
    if (memento_) {
        editor_->restoreFromMemento(memento_);
    }
}

private: TextEditor editor; std::string text; Memento memento_ = nullptr; };

使用命令的历史管理器来支持多级撤销:

class CommandHistory {
public:
    void push(Command* command) {
        command->execute();
        history.push_back(command);
    }
void undo() {
    if (!history.empty()) {
        Command* command = history.back();
        command->undo();
        history.pop_back();
        delete command;
    }
}

~CommandHistory() {
    for (auto cmd : history) {
        delete cmd;
    }
}

private: std::vector history; };

实际使用示例

将这些组件组合起来:

int main() {
    TextEditor editor;
    CommandHistory history;
history.push(new InsertCommand(&editor, "Hello"));
std::cout << editor.getState() << "\n"; // 输出: Hello

history.push(new InsertCommand(&editor, " World"));
std::cout << editor.getState() << "\n"; // 输出: Hello World

history.undo();
std::cout << editor.getState() << "\n"; // 输出: Hello

history.undo();
std::cout << editor.getState() << "\n"; // 输出: (空)

return 0;

}

每次执行命令前保存状态,undo 时恢复,实现了可靠的撤销功能。

基本上就这些。关键在于命令对象持有执行前的备忘录,在 undo 时利用它还原状态。这种方式结构清晰,扩展性强,适合需要多级撤销/重做的应用,比如编辑器、图形软件或游戏指令系统。