备忘录模式通过发起者创建、管理者保存、备忘录存储状态实现对象状态的保存与恢复,适用于撤销、回滚等场景,如文本编辑器;关键在于私有化备忘录构造函数并用友元保证封装性,使用栈管理多级撤销,注意内存开销与深拷贝问题。

在C++中实现备忘录模式,核心是捕获一个对象的内部状态,并在不破坏封装性的前提下将其保存,以便后续恢复。这种设计常用于需要支持撤销、回滚或历史记录功能的场景,比如文本编辑器、游戏存档、配置管理等。
备忘录模式的角色组成
备忘录模式包含三个基本角色:
- 发起者(Originator):拥有内部状态,能够创建备忘录来保存当前状态,也能通过备忘录恢复状态。
- 备忘录(Memento):负责存储发起者的内部状态。通常将构造函数设为私有,仅允许发起者访问其内容。
- 管理者(Caretaker):负责保存和管理备忘录,但不能修改或查看其内容。
实现步骤与代码示例
以下是一个简单的C++实现,模拟文本编辑器的撤销功能:
#include#include #include class Memento; // 发起者:文本编辑器 class TextEditor { private: std::string content; public: void write(const std::string& text) { content += text; } std::string getContent() const { return content; } // 创建备忘录 Memento createMemento() const; // 恢复状态 void restoreFromMemento(const Memento& m); }; // 备忘录类 class Memento { friend class TextEditor; // 允许TextEditor访问私有成员 private: std::string state; // 私有构造函数,仅发起者可创建 Memento(const std::string& state) : state(state) {} public: std::string getState() const { return state; } }; // 成员函数实现在Memento定义之后 Memento TextEditor::createMemento() const { return Memento(content); } void TextEditor::restoreFromMemento(const Memento& m) { content = m.state; } // 管理者:负责保存和恢复备忘录 class History { private: std::stack snapshots; public: void push(const Memento& m) { snapshots.push(m); } Memento pop() { if (snapshots.empty()) { throw std::runtime_error("No snapshots available"); } Memento m = snapshots.top(); snapshots.pop(); return m; } bool empty() const { return snapshots.empty(); } };
使用示例:
立即学习“C++免费学习笔记(深入)”;
int main() {
TextEditor editor;
History history;
editor.write("Hello ");
history.push(editor.createMemento()); // 保存状态
editor.write("World!");
std::cout << "Current: " << editor.getContent() << "\n";
editor.restoreFromMemento(history.pop()); // 撤销
std::cout << "After undo: " << editor.getContent() << "\n";
return 0;
}
关键设计要点
实现时需注意以下几点以保证封装性和安全性:
- 将
Memento的构造函数设为私有,防止外部直接构造非法状态。 - 使用
friend关键字让发起者能访问备忘录的私有数据,而管理者只能存储和传递,无法查看或修改。 - 若状态较大,可考虑使用指针或共享所有权(如
std::shared_ptr)避免频繁拷贝。 - 支持多级撤销时,可用栈结构保存多个备忘录;若需内存优化,可引入快照压缩或差量存储。
适用场景与注意事项
备忘录模式适合用于需要精确回滚到历史状态的场合。但它会带来一定的内存开销,尤其是状态大或保存频繁时。应根据实际需求控制保存频率,或设置最大保存数量。
如果对象状态无法被安全复制(如包含资源句柄),需实现深拷贝逻辑。对于复杂对象,也可结合序列化机制保存状态。
基本上就这些。只要把握好封装边界,C++中的备忘录模式可以很清晰地实现状态保存与恢复。










