马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
备忘录模式详解
一、备忘录模式概述
备忘录模式(Memento Pattern)是一种行为型设计模式,它答应在不破坏封装性的条件下捕捉并外部化一个对象的内部状态,以便以后可以将该对象恢复到原先保存的状态。
核心特点
- 状态保存:捕捉对象内部状态
- 封装掩护:不暴露对象实现细节
- 打消机制:支持回滚到之前状态
- 汗青管理:可维护多个状态快照
二、备忘录模式的结构
主要脚色
- Originator:原发器,需要保存状态的对象
- Memento:备忘录,存储原发器状态
- Caretaker:管理者,负责保存和恢复备忘录
三、备忘录模式的实现
1. 基本实现
- // 备忘录类
- public class TextMemento {
- private final String text;
-
- public TextMemento(String text) {
- this.text = text;
- }
-
- public String getText() {
- return text;
- }
- }
- // 原发器 - 文本编辑器
- public class TextEditor {
- private String text;
-
- public void write(String text) {
- this.text = text;
- }
-
- public TextMemento save() {
- return new TextMemento(text);
- }
-
- public void restore(TextMemento memento) {
- this.text = memento.getText();
- }
-
- public void print() {
- System.out.println("当前内容: " + text);
- }
- }
- // 管理者
- public class History {
- private Stack<TextMemento> history = new Stack<>();
-
- public void push(TextMemento memento) {
- history.push(memento);
- }
-
- public TextMemento pop() {
- return history.pop();
- }
- }
- // 使用示例
- TextEditor editor = new TextEditor();
- History history = new History();
- editor.write("第一版内容");
- history.push(editor.save());
- editor.print();
- editor.write("修改后的内容");
- editor.print();
- editor.restore(history.pop());
- editor.print(); // 恢复为"第一版内容"
复制代码 2. 更复杂的实现(支持多个属性)
- // 游戏角色状态
- public class GameCharacter {
- private int level;
- private int health;
- private String location;
-
- public GameCharacter(int level, int health, String location) {
- this.level = level;
- this.health = health;
- this.location = location;
- }
-
- // 创建备忘录
- public CharacterMemento save() {
- return new CharacterMemento(level, health, location);
- }
-
- // 从备忘录恢复
- public void restore(CharacterMemento memento) {
- this.level = memento.getLevel();
- this.health = memento.getHealth();
- this.location = memento.getLocation();
- }
-
- // 其他方法...
- }
- // 角色备忘录
- public class CharacterMemento {
- private final int level;
- private final int health;
- private final String location;
-
- public CharacterMemento(int level, int health, String location) {
- this.level = level;
- this.health = health;
- this.location = location;
- }
-
- // getter方法
- public int getLevel() { return level; }
- public int getHealth() { return health; }
- public String getLocation() { return location; }
- }
复制代码 四、备忘录模式的应用场景
1. 文本编辑器打消功能
- public class AdvancedTextEditor {
- private StringBuilder content;
- private int cursorPosition;
-
- public AdvancedTextEditor() {
- content = new StringBuilder();
- cursorPosition = 0;
- }
-
- public EditorState saveState() {
- return new EditorState(content.toString(), cursorPosition);
- }
-
- public void restoreState(EditorState state) {
- this.content = new StringBuilder(state.getContent());
- this.cursorPosition = state.getCursorPosition();
- }
-
- // 其他编辑方法...
- }
- public class EditorState {
- private final String content;
- private final int cursorPosition;
-
- public EditorState(String content, int cursorPosition) {
- this.content = content;
- this.cursorPosition = cursorPosition;
- }
-
- // getter方法...
- }
复制代码 2. 游戏存档体系
- public class GameSaveManager {
- private List<GameSave> savePoints = new ArrayList<>();
-
- public void saveGame(GameWorld world) {
- savePoints.add(world.createSave());
- }
-
- public void loadGame(GameWorld world, int index) {
- if (index >= 0 && index < savePoints.size()) {
- world.restoreFromSave(savePoints.get(index));
- }
- }
- }
- public class GameWorld {
- private String worldState;
- private PlayerState playerState;
-
- public GameSave createSave() {
- return new GameSave(worldState, playerState);
- }
-
- public void restoreFromSave(GameSave save) {
- this.worldState = save.getWorldState();
- this.playerState = save.getPlayerState();
- }
- }
复制代码 3. 事务回滚机制
- public class DatabaseTransaction {
- private List<DatabaseMemento> mementos = new ArrayList<>();
- private Database database;
-
- public void begin() {
- mementos.add(database.createMemento());
- }
-
- public void commit() {
- mementos.clear();
- }
-
- public void rollback() {
- if (!mementos.isEmpty()) {
- database.restoreFromMemento(mementos.get(mementos.size() - 1));
- mementos.remove(mementos.size() - 1);
- }
- }
- }
复制代码 五、备忘录模式的变体
1. 增量备忘录
- public class IncrementalMemento {
- private final Object deltaState; // 只存储变化的部分
-
- public IncrementalMemento(Object delta) {
- this.deltaState = delta;
- }
- }
复制代码 2. 序列化备忘录
- import java.io.*;
- public class SerializationMemento {
- public static byte[] serialize(Object obj) throws IOException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream(bos);
- oos.writeObject(obj);
- return bos.toByteArray();
- }
-
- public static Object deserialize(byte[] data) throws IOException, ClassNotFoundException {
- ByteArrayInputStream bis = new ByteArrayInputStream(data);
- ObjectInputStream ois = new ObjectInputStream(bis);
- return ois.readObject();
- }
- }
复制代码 六、备忘录模式的优缺点
优点
- 状态封装:不暴露对象内部细节
- 简化原发器:状态保存恢复逻辑分离
- 时间点恢复:可恢复到任意保存点
- 快照管理:可维护多个状态版本
缺点
- 内存消耗:保存多个状态大概占用大量内存
- 性能影响:频繁保存状态大概影响性能
- 实现复杂:部门对象状态大概难以捕捉
七、最佳实践
- 公道控制快照数目:制止保存过多状态
- 增量保存:只保存变化的部门
- 考虑序列化:对于复杂对象使用序列化
- 清算机制:定期清算逾期备忘录
- 访问控制:限制对备忘录内部状态的访问
八、总结
备忘录模式是状态管理的有用方案,特别适用于:
- 需要提供打消/重做功能
- 需要保存对象汗青状态
- 需要实现事务回滚机制
- 需要保存体系快照
在实际开辟中,备忘录模式常见于:
- 文本/图像编辑软件
- 游戏存档体系
- 数据库事务管理
- 体系设置管理
- 工作流状态保存
正确使用备忘录模式可以实现机动的状态管理,但需要注意内存和性能方面的考量。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |