九折技术 九折技术
首页
  • Go
  • MIT-6824
  • 算法与数据结构
  • 面向对象
  • 代码整洁
  • 重构
  • 设计模式
  • 学习
  • 技术
  • 人文
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub

HoldDie

长期有耐心,一切才刚刚开始!
首页
  • Go
  • MIT-6824
  • 算法与数据结构
  • 面向对象
  • 代码整洁
  • 重构
  • 设计模式
  • 学习
  • 技术
  • 人文
关于
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub
  • 代码整洁

  • 重构

  • 设计模式

    • 创建型-单例模式
    • 创建型-工厂模式
    • 创建型-建造者模式
    • 创建型-原型模式
    • 结构型-代理模式
    • 结构型-桥接模式
    • 结构型-装饰器模式
    • 结构型-适配器模式
    • 结构型-门面模式
    • 结构型-组合模式
    • 结构型-享元模式
    • 行为型-观察者模式
    • 行为型-模板模式
    • 行为型-策略模式
    • 行为型-责任链模式
    • 行为型-状态模式
    • 行为型-迭代器模式
    • 行为型-访问者模式
    • 行为型-备忘录模式
      • 行为型-命令模式
      • 行为型-解释器模式
      • 行为型-中介模式
    • 架构
    • 设计模式
    holddie
    2020-11-24

    行为型-备忘录模式

    # 备忘录模式

    • 备忘录模式主要用来防止丢失、撤销、恢复等。
    • 备忘录模式,也称快照模式(Snapshot),在不违背封装原则的前提下,捕获一个对象的内部状态,并在改对象之外保存这个状态,以便之后恢复对象为先先前的状态。
    • 为什么存储和恢复副本会违背封装原则?
    • 备忘录模式是如何做到不违背封装原则的?
    • 当我们需要恢复到某一时间点的备份的时候,如果这一时间点有做全量备份,我们直接拿来恢复就可以了。如果这一时间点没有对应的全量备份,我们就先找到最近的一次全量备份,然后用它来恢复,之后执行此次全量备份跟这一时间点之间的所有增量备份,也就是对应的操作或者数据变动。
    • 备忘录模式更侧重于代码的设计和实现,备份则更侧重架构设计或产品设计。
    
    public class InputText {
      private StringBuilder text = new StringBuilder();
    
      public String getText() {
        return text.toString();
      }
    
      public void append(String input) {
        text.append(input);
      }
    
      public Snapshot createSnapshot() {
        return new Snapshot(text.toString());
      }
    
      public void restoreSnapshot(Snapshot snapshot) {
        this.text.replace(0, this.text.length(), snapshot.getText());
      }
    }
    
    public class Snapshot {
      private String text;
    
      public Snapshot(String text) {
        this.text = text;
      }
    
      public String getText() {
        return this.text;
      }
    }
    
    public class SnapshotHolder {
      private Stack<Snapshot> snapshots = new Stack<>();
    
      public Snapshot popSnapshot() {
        return snapshots.pop();
      }
    
      public void pushSnapshot(Snapshot snapshot) {
        snapshots.push(snapshot);
      }
    }
    
    public class ApplicationMain {
      public static void main(String[] args) {
        InputText inputText = new InputText();
        SnapshotHolder snapshotsHolder = new SnapshotHolder();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
          String input = scanner.next();
          if (input.equals(":list")) {
            System.out.println(inputText.toString());
          } else if (input.equals(":undo")) {
            Snapshot snapshot = snapshotsHolder.popSnapshot();
            inputText.restoreSnapshot(snapshot);
          } else {
            snapshotsHolder.pushSnapshot(inputText.createSnapshot());
            inputText.append(input);
          }
        }
      }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    编辑
    #备忘录模式
    上次更新: 2020/11/29, 15:11:00
    行为型-访问者模式
    行为型-命令模式

    ← 行为型-访问者模式 行为型-命令模式→

    最近更新
    01
    行为型-访问者模式
    11-24
    02
    行为型-命令模式
    11-24
    03
    行为型-解释器模式
    11-24
    更多文章>
    Theme by Vdoing | Copyright © 2019-2020 HoldDie | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式