备忘录模式

1、场景

1、棋类游戏中的,悔棋

2、 普通软件中的,撤销操作

3、 数据库软件中的,事务管理中的,回滚操作

4、 Photoshop软件中的,历史记录

2、核心

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

3、结构

  • 源发器类 Originator
  • 备忘录类 Memento
  • 负责人类 CareTaker

4、代码实现

4.1、源发器类

java 复制代码
/**
 * 源发器类
 */
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 +
                '}';
    }
}

4.2、备忘录类

java 复制代码
/**
 * 备忘录类
 */
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;
    }
}

4.3、负责人类

java 复制代码
/**
 * 负责人类
 * 负责管理备忘录对象
 */
public class CareTaker {
    private EmpMemento empMemento;
//    private List<EmpMemento> list = new ArrayList<>();
    public EmpMemento getEmpMemento() {
        return empMemento;
    }
    public void setEmpMemento(EmpMemento empMemento) {
        this.empMemento = empMemento;
    }
}

4.4、测试结果

5、负责人类注意点

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

java 复制代码
private List<EmpMemento> list = new ArrayList<>();

备忘点较多时:

  • 可以将备忘点压栈
java 复制代码
public class CareTaker {

		private Memento memento;
		
		private Stack<Memento> stack = new Stack<Memento>();
	}
  • 将多个备忘录对象,序列化和持久化

6、代码UML图

相关推荐
人月神话-Lee16 小时前
【图像处理】框架设计——协议、值类型与工程化思维
图像处理·人工智能·ios·设计模式·架构·ai编程·swift
AI大法师17 小时前
Xbox回归经典绿
大数据·设计模式·xbox
老码观察17 小时前
设计模式实战解读(六):装饰器模式——功能增强,不动原代码
java·设计模式·装饰器模式
Doris_20231 天前
代码格式化 使用oxfmt
设计模式·架构·前端框架
Doris_20231 天前
说一说ESLint+Prettier生效的原理
前端·设计模式·架构
Pomelooooo1 天前
把 git commit 这件事,彻底交给 AI ——一个工程化 /git-commit 命令的设计与落地
设计模式
invicinble1 天前
设计模式(类的拓扑结构)(描述总纲)
设计模式·原型模式
invicinble2 天前
设计模式(类的拓扑结构)(为什么会产生设计模式,以及什么是设计模式)
linux·服务器·设计模式
PersonalViolet2 天前
模板方法模式实战:重构Agent工具审批,告别重复代码
设计模式·agent