西河刘卡车医 发表于 2025-4-12 17:52:15

筹划模式之备忘录模式:对象状态的可逆时光机

弁言

备忘录模式(Memento Pattern)是一种行为型筹划模式,它允许在不破坏封装性的前提下捕获并外部化对象的内部状态,以便后续可以规复到该状态。这种模式就像为对象提供了一个"时光机",让我们能够回溯到先前的状态。本文将深入解析备忘录模式的原理、实现方式以及典型应用场景。
1. 备忘录模式的核心概念

1.1 什么是备忘录模式?

备忘录模式通过三个核心角色实现状态保存:


[*] Originator(原发器):需要保存状态的对象
[*] Memento(备忘录):存储原发器状态的不可变对象
[*] Caretaker(管理者):负责保存和管理备忘录
1.2 典型应用场景



[*] 打消/重做功能(如文本编辑器)
[*] 游戏存档/读档
[*] 事务回滚机制
[*] 配置历史纪录
2. 备忘录模式的实现方式

2.1 基本布局实现

// 备忘录接口(窄接口,对外隐藏细节)
public interface Memento {
    // 不暴露任何方法
}

// 原发器
class TextEditor {
    private String content;
   
    // 创建备忘录
    public Memento save() {
      return new TextMemento(content);
    }
   
    // 恢复状态
    public void restore(Memento memento) {
      TextMemento tm = (TextMemento)memento;
      this.content = tm.getSavedContent();
    }
   
    // 内部备忘录实现
    private static class TextMemento implements Memento {
      private final String content;
      
      private TextMemento(String content) {
            this.content = content;
      }
      
      private String getSavedContent() {
            return content;
      }
    }
}

// 管理者
class History {
    private Stack<Memento> stack = new Stack<>();
   
    public void push(Memento memento) {
      stack.push(memento);
    }
   
    public Memento pop() {
      return stack.pop();
    }
} 2.2 进阶实现本领


[*] 增量备忘录:只保存变化的部分状态
[*] 长期化备忘录:将状态保存到数据库/文件
[*] 深拷贝实现:使用序列化实现深度状态保存
3. 备忘录模式的最佳实践

3.1 封装性掩护



[*] 使用内部类实现备忘录
[*] 为备忘录筹划窄接口
[*] 限定管理者对备忘录内容的访问
3.2 性能优化



[*] 大对象考虑使用增量保存
[*] 设置历史纪录上限
[*] 实现懒加载机制
3.3 与其他模式团结



[*] 与下令模式实现可打消操作
[*] 与原型模式实现状态克隆
[*] 与观察者模式实现状态变更关照
4. 备忘录模式的实际应用

4.1 文档编辑器的打消功能

// 完整实现示例
class Document {
    private StringBuilder content = new StringBuilder();
    private History history = new History();
   
    public void write(String text) {
      history.push(createMemento());
      content.append(text);
    }
   
    public void undo() {
      if (history.hasStates()) {
            restoreFromMemento(history.pop());
      }
    }
   
    private Memento createMemento() {
      return new DocumentMemento(content.toString());
    }
   
    private void restoreFromMemento(Memento memento) {
      DocumentMemento dm = (DocumentMemento)memento;
      content = new StringBuilder(dm.getState());
    }
   
    private static class DocumentMemento implements Memento {
      private final String state;
      
      private DocumentMemento(String state) {
            this.state = state;
      }
      
      private String getState() {
            return state;
      }
    }
}

class History {
    private Deque<Memento> states = new ArrayDeque<>();
    private static final int MAX_HISTORY = 10;
   
    public void push(Memento state) {
      if (states.size() == MAX_HISTORY) {
            states.removeFirst();
      }
      states.push(state);
    }
   
    public Memento pop() {
      return states.pop();
    }
   
    public boolean hasStates() {
      return !states.isEmpty();
    }
} 4.2 游戏角色状态存档

class GameCharacter {
    private int health;
    private int level;
    private Position position;
   
    public GameSave save() {
      return new GameSave(health, level, position.clone());
    }
   
    public void load(GameSave save) {
      this.health = save.getHealth();
      this.level = save.getLevel();
      this.position = save.getPosition().clone();
    }
   
    // 备忘录实现
    public static class GameSave {
      private final int health;
      private final int level;
      private final Position position;
      
      private GameSave(int health, int level, Position position) {
            this.health = health;
            this.level = level;
            this.position = position;
      }
      
      // getters...
    }
} 4.3 表单数据主动保存

class FormData {
    private Map<String, Object> fields = new HashMap<>();
    private SaveManager saveManager = new SaveManager();
   
    public void setField(String name, Object value) {
      saveManager.save(this.createSnapshot());
      fields.put(name, value);
    }
   
    public void restorePrevious() {
      FormSnapshot snapshot = saveManager.getLastSnapshot();
      if (snapshot != null) {
            this.fields = snapshot.getFields();
      }
    }
   
    private FormSnapshot createSnapshot() {
      return new FormSnapshot(new HashMap<>(fields));
    }
   
    private static class FormSnapshot {
      private final Map<String, Object> fields;
      
      private FormSnapshot(Map<String, Object> fields) {
            this.fields = fields;
      }
      
      private Map<String, Object> getFields() {
            return new HashMap<>(fields);
      }
    }
} 5. 备忘录模式的优缺点分析

5.1 优势



[*] 完整状态保存:可以精确规复到任意历史状态
[*] 封装掩护:不破坏原发器的封装性
[*] 简化原发器代码:将状态管理职责分离
5.2 局限性



[*] 内存斲丧:保存大量状态可能斲丧内存
[*] 大对象性能题目:频仍保存大对象状态可能影响性能
[*] 实现复杂度:需要合理筹划备忘录的存储布局
结语

备忘录模式为对象状态管理提供了优雅的解决方案,特别适合需要实现打消、历史纪录或事务回滚的场景。通过合理应用备忘录模式,可以增强系统的机动性和用户体验。在实际开发中,需要根据具体场景权衡内存使用和功能需求,须要时可团结其他模式进行优化。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 筹划模式之备忘录模式:对象状态的可逆时光机