• 备忘录模式


    1、场景

    1、棋类游戏中的,悔棋
    2、 普通软件中的,撤销操作
    3、 数据库软件中的,事务管理中的,回滚操作
    4、 Photoshop软件中的,历史记录

    2、核心

    保存某个对象内部状态的拷贝,以后就可以将该对象恢复到原先的状态。

    3、结构

    • 源发器类 Originator
    • 备忘录类 Memento
    • 负责人类 CareTaker
      在这里插入图片描述

    4、代码实现

    4.1、源发器类
    /**
     * 源发器类
     */
    public class Emp {
        private String name;
        private int age;
        private double salary;
    
        //进行备忘操作,并返回备忘录对象
        public EmpMemento memento(){
            return new EmpMemento(this);
        }
        //进行数据恢复,恢复成制定备忘录对象的值
        public void recovery(EmpMemento memento){
            this.name = memento.getName();
            this.age = memento.getAge();
            this.salary = memento.getSalary();
        }
        public Emp(String name, int age, double salary) {
            this.name = name;
            this.age = age;
            this.salary = salary;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public double getSalary() {
            return salary;
        }
        public void setSalary(double salary) {
            this.salary = salary;
        }
        @Override
        public String toString() {
            return "Emp{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", salary=" + salary +
                    '}';
        }
    }
    
    • 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
    4.2、备忘录类
    /**
     * 备忘录类
     */
    public class EmpMemento {
        private String name;
        private int age;
        private double salary;
    
        public EmpMemento(Emp emp) {
            this.name = emp.getName();
            this.age = emp.getAge();
            this.salary = emp.getSalary();
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public double getSalary() {
            return salary;
        }
        public void setSalary(double salary) {
            this.salary = salary;
        }
    }
    
    
    • 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
    4.3、负责人类
    /**
     * 负责人类
     * 负责管理备忘录对象
     */
    public class CareTaker {
        private EmpMemento empMemento;
    //    private List list = new ArrayList<>();
        public EmpMemento getEmpMemento() {
            return empMemento;
        }
        public void setEmpMemento(EmpMemento empMemento) {
            this.empMemento = empMemento;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    4.4、测试结果

    在这里插入图片描述

    5、负责人类注意点

    在负责人类负责保存好的备忘录对象。可以通过增加容器,设置多个“备忘点”

    private List<EmpMemento> list = new ArrayList<>();
    
    • 1

    备忘点较多时:

    • 可以将备忘点压栈
    public class CareTaker {
    
    		private Memento memento;
    		
    		private Stack<Memento> stack = new Stack<Memento>();
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 将多个备忘录对象,序列化和持久化

    6、代码UML图

    在这里插入图片描述

  • 相关阅读:
    【前端】Vue+Element UI案例:通用后台管理系统-Echarts图表:折线图、柱状图、饼状图
    国军标9001c质量管理体系认证条件
    List.of() 与 Arrays.asList()总结
    Android基础面试题
    【C语言】进制转换一般方法
    码农的转型之路-偶遇大佬情况或有变
    面试高频问题----2
    解决git在window11操作很慢,占用很大cpu的问题
    2024年全国青少信息素养大赛python编程复赛集训第十二天编程题分享
    Android 自定义SeekBar显示进度百分比
  • 原文地址:https://blog.csdn.net/weixin_42072357/article/details/132722477