C++中的备忘录模式

目录

[备忘录模式(Memento Pattern)](#备忘录模式(Memento Pattern))

实际应用

文本编辑器的撤销功能

游戏角色状态保存和恢复

图形编辑器的撤销/重做功能

总结


备忘录模式(Memento Pattern)

备忘录模式是一种行为型设计模式,它允许在不破坏封装性的前提下捕获和恢复对象的内部状态。这个模式的主要目的是保存对象的某个状态,以便在适当的时候恢复对象到之前的状态。备忘录模式主要包含三个角色:

  1. Originator :负责创建一个备忘录(Memento ),用以记录当前的内部状态,并在需要时使用备忘录恢复内部状态。

  2. Memento :备忘录,负责存储Originator 的内部状态。

  3. Caretaker:负责保存备忘录,但不能对备忘录的内容进行操作或检查。

实际应用

文本编辑器的撤销功能

-- 文本编辑器,支持撤销操作。

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>

// Memento类,用于存储文本编辑器的状态
class Memento {
private:
    std::string state;

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

// Originator类,表示文本编辑器
class TextEditor {
private:
    std::string text;

public:
    void write(const std::string& words) {
        text += words;
    }

    std::string getText() const {
        return text;
    }

    Memento save() const {
        return Memento(text);
    }

    void restore(const Memento& memento) {
        text = memento.getState();
    }
};

// Caretaker类,用于管理Memento
class Caretaker {
private:
    std::vector<Memento> history;

public:
    void save(const Memento& memento) {
        history.push_back(memento);
    }

    Memento undo() {
        if (!history.empty()) {
            Memento memento = history.back();
            history.pop_back();
            return memento;
        }
        return Memento(""); // 返回空的Memento
    }
};

// 客户端代码
int main() {
    TextEditor editor;
    Caretaker caretaker;

    editor.write("Hello ");
    caretaker.save(editor.save());

    editor.write("World!");
    caretaker.save(editor.save());

    std::cout << "Current Text: " << editor.getText() << std::endl;

    editor.restore(caretaker.undo());
    std::cout << "After Undo: " << editor.getText() << std::endl;

    editor.restore(caretaker.undo());
    std::cout << "After Undo: " << editor.getText() << std::endl;

    return 0;
}

游戏角色状态保存和恢复

-- 模拟一个游戏角色的状态保存和恢复功能。

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>

// Memento类,用于存储游戏角色的状态
class Memento {
private:
    int health;
    int mana;
    int experience;

public:
    Memento(int health, int mana, int experience)
        : health(health), mana(mana), experience(experience) {}

    int getHealth() const { return health; }
    int getMana() const { return mana; }
    int getExperience() const { return experience; }
};

// Originator类,表示游戏角色
class GameCharacter {
private:
    int health;
    int mana;
    int experience;

public:
    GameCharacter(int health, int mana, int experience)
        : health(health), mana(mana), experience(experience) {}

    void setState(int health, int mana, int experience) {
        this->health = health;
        this->mana = mana;
        this->experience = experience;
    }

    void displayState() const {
        std::cout << "Health: " << health << ", Mana: " << mana
                  << ", Experience: " << experience << std::endl;
    }

    Memento save() const {
        return Memento(health, mana, experience);
    }

    void restore(const Memento& memento) {
        health = memento.getHealth();
        mana = memento.getMana();
        experience = memento.getExperience();
    }
};

// Caretaker类,用于管理Memento
class Caretaker {
private:
    std::vector<Memento> savepoints;

public:
    void save(const Memento& memento) {
        savepoints.push_back(memento);
    }

    Memento load() {
        if (!savepoints.empty()) {
            Memento memento = savepoints.back();
            savepoints.pop_back();
            return memento;
        }
        return Memento(100, 100, 0); // 返回初始状态
    }
};

// 客户端代码
int main() {
    GameCharacter character(100, 50, 0);
    Caretaker caretaker;

    character.displayState();
    caretaker.save(character.save());

    character.setState(80, 40, 10);
    character.displayState();
    caretaker.save(character.save());

    character.setState(50, 30, 20);
    character.displayState();

    character.restore(caretaker.load());
    character.displayState();

    character.restore(caretaker.load());
    character.displayState();

    return 0;
}

图形编辑器的撤销/重做功能

-- 图形编辑器,支持撤销和重做操作。

cpp 复制代码
#include <iostream>
#include <vector>
#include <string>
#include <stack>

// Memento类,用于存储图形编辑器的状态
class Memento {
private:
    std::vector<std::string> shapes;

public:
    Memento(const std::vector<std::string>& shapes) : shapes(shapes) {}
    std::vector<std::string> getState() const { return shapes; }
};

// Originator类,表示图形编辑器
class GraphicEditor {
private:
    std::vector<std::string> shapes;

public:
    void addShape(const std::string& shape) {
        shapes.push_back(shape);
    }

    void displayShapes() const {
        for (const auto& shape : shapes) {
            std::cout << shape << " ";
        }
        std::cout << std::endl;
    }

    Memento save() const {
        return Memento(shapes);
    }

    void restore(const Memento& memento) {
        shapes = memento.getState();
    }
};

// Caretaker类,用于管理Memento
class Caretaker {
private:
    std::stack<Memento> undoStack;
    std::stack<Memento> redoStack;

public:
    void save(const Memento& memento) {
        undoStack.push(memento);
        while (!redoStack.empty()) {
            redoStack.pop();
        }
    }

    Memento undo() {
        if (!undoStack.empty()) {
            Memento memento = undoStack.top();
            undoStack.pop();
            redoStack.push(memento);
            return memento;
        }
        return Memento(std::vector<std::string>()); // 返回空的Memento
    }

    Memento redo() {
        if (!redoStack.empty()) {
            Memento memento = redoStack.top();
            redoStack.pop();
            undoStack.push(memento);
            return memento;
        }
        return Memento(std::vector<std::string>()); // 返回空的Memento
    }
};

// 客户端代码
int main() {
    GraphicEditor editor;
    Caretaker caretaker;

    editor.addShape("Circle");
    caretaker.save(editor.save());

    editor.addShape("Square");
    caretaker.save(editor.save());

    editor.addShape("Triangle");
    caretaker.save(editor.save());

    std::cout << "Current Shapes: ";
    editor.displayShapes();

    editor.restore(caretaker.undo());
    std::cout << "After Undo: ";
    editor.displayShapes();

    editor.restore(caretaker.undo());
    std::cout << "After Undo: ";
    editor.displayShapes();

    editor.restore(caretaker.redo());
    std::cout << "After Redo: ";
    editor.displayShapes();

    editor.restore(caretaker.redo());
    std::cout << "After Redo: ";
    editor.displayShapes();

    return 0;
}

总结

备忘录模式帮助我们在不破坏对象封装性的前提下捕获和恢复对象的内部状态,从而实现撤销和重做操作。

相关推荐
虫小宝6 分钟前
如何在Java中实现PDF生成
java·开发语言·pdf
菜鸡且互啄691 小时前
在线教育平台,easyexcel使用案例
java·开发语言
电饭叔2 小时前
《python程序语言设计》2018版第5章第52题利用turtle绘制sin函数
开发语言·python
weixin_452600692 小时前
如何为老化的汽车铅酸电池充电
开发语言·单片机·安全·汽车·电机·电源模块·充电桩
Java资深爱好者3 小时前
如何在std::map中查找元素
开发语言·c++
YCCX_XFF213 小时前
ImportError: DLL load failed while importing _imaging: 操作系统无法运行 %1
开发语言·python
哥廷根数学学派4 小时前
基于Maximin的异常检测方法(MATLAB)
开发语言·人工智能·深度学习·机器学习
杰哥在此5 小时前
Java面试题:讨论持续集成/持续部署的重要性,并描述如何在项目中实施CI/CD流程
java·开发语言·python·面试·编程
Unity打怪升级5 小时前
Laravel: 优雅构建PHP应用的现代框架
开发语言·php·laravel
C.C6 小时前
java IO流(1)
java·开发语言