• Java设计模式之备忘录模式


    备忘录模式(Memento Pattern)是一种行为型设计模式,它允许在不暴露对象内部状态的情况下捕获和恢复对象的内部状态。该模式通过在对象之外保存和恢复对象的状态,使得对象可以在需要时回滚到之前的状态。

    在备忘录模式中,有三个核心角色:

    1. 发起人(Originator):它是需要保存状态的对象。它可以创建一个备忘录对象,用于保存当前状态,并可以使用备忘录对象恢复其状态。
    2. 备忘录(Memento):它是保存发起人对象状态的对象。备忘录对象提供一个接口,允许发起人对象访问其内部状态(或者在某些情况下,允许其他对象访问)。
    3. 管理者(Caretaker):它负责保存和恢复备忘录对象。管理者对象可以存储多个备忘录对象,并在需要时将其提供给发起人对象。

    下面是一个示例,展示了如何使用备忘录模式来保存和恢复发起人对象的状态。假设我们有一个文本编辑器,用户可以输入文本并进行撤销操作。

    // 发起人(Originator)
    class TextEditor {
        private String text;
    
        public void setText(String text) {
            this.text = text;
        }
    
        public String getText() {
            return text;
        }
    
        public TextEditorMemento save() {
            return new TextEditorMemento(text);
        }
    
        public void restore(TextEditorMemento memento) {
            this.text = memento.getText();
        }
    }
    
    // 备忘录(Memento)
    class TextEditorMemento {
        private String text;
    
        public TextEditorMemento(String text) {
            this.text = text;
        }
    
        public String getText() {
            return text;
        }
    }
    
    // 管理者(Caretaker)
    class TextEditorHistory {
        private Stack<TextEditorMemento> history = new Stack<>();
    
        public void push(TextEditorMemento memento) {
            history.push(memento);
        }
    
        public TextEditorMemento pop() {
            return history.pop();
        }
    }
    
    // 示例使用
    public class Main {
        public static void main(String[] args) {
            TextEditor textEditor = new TextEditor();
            TextEditorHistory history = new TextEditorHistory();
    
            // 编辑文本
            textEditor.setText("Hello, World!");
    
            // 保存状态
            history.push(textEditor.save());
    
            // 修改文本
            textEditor.setText("Hello, Java!");
    
            // 保存状态
            history.push(textEditor.save());
    
            // 恢复到之前的状态
            textEditor.restore(history.pop());
            System.out.println(textEditor.getText());  // 输出: Hello, Java!
    
            textEditor.restore(history.pop());
            System.out.println(textEditor.getText());  // 输出: Hello, World!
        }
    }
    
    • 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
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    在上面的示例中,TextEditor 是发起人角色,它保存了文本编辑器的状态,并提供了保存和恢复状态的方法。TextEditorMemento 是备忘录角色,它保存了发起人对象的状态。TextEditorHistory 是管理者角色,它保存了多个备忘录对象,并提供了保存和恢复备忘录的方法。通过使用备忘录模式,我们可以在文本编辑器中保存多个状态,并在需要时恢复到之前的状态。

    推荐一个ChatGPT使用渠道:点击直达
  • 相关阅读:
    链表快慢指针合集(力扣)
    爆款自媒体带货脚本,大V不外传的流量密码
    为什么要做数据可视化
    Docker安装MySQL
    css初学之css基础用法及选择器(二)
    解决npm ERR! Cannot read properties of null (reading ‘pickAlgorithm‘)
    Kafka3.x核心速查手册二、客户端使用篇-6、消息发送幂等性
    基线加固是什么
    loadrunner lr解决参数化一次取多条记录【一对多问题】
    图扑数字孪生智慧加油站,构建安全防护网
  • 原文地址:https://blog.csdn.net/kkwyting/article/details/133875638