目录
备忘录模式(Memento Pattern)
备忘录模式是一种行为型设计模式,它允许在不破坏封装性的前提下捕获和恢复对象的内部状态。这个模式的主要目的是保存对象的某个状态,以便在适当的时候恢复对象到之前的状态。备忘录模式主要包含三个角色:
1. Originator:负责创建一个备忘录(Memento),用以记录当前的内部状态,并在需要时使用备忘录恢复内部状态。
2. Memento:备忘录,负责存储Originator的内部状态。
3. Caretaker:负责保存备忘录,但不能对备忘录的内容进行操作或检查。
实际应用
文本编辑器的撤销功能
-- 文本编辑器,支持撤销操作。
#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;
}
游戏角色状态保存和恢复
-- 模拟一个游戏角色的状态保存和恢复功能。
#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;
}
图形编辑器的撤销/重做功能
-- 图形编辑器,支持撤销和重做操作。
#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;
}
总结
备忘录模式帮助我们在不破坏对象封装性的前提下捕获和恢复对象的内部状态,从而实现撤销和重做操作。