备忘录模式

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图

相关推荐
沫璃染墨2 小时前
《从零入门Linux系统篇(五十六):线程篇·九——生产者消费者模型:从条件变量到BlockingQueue实现》
linux·服务器·c++·驱动开发·设计模式·架构·系统架构
c萱1 天前
AI产品经理——01基础认知篇
设计模式·prompt·aigc·产品经理·ai编程·gitcode·ai-native
YYYing.1 天前
【设计模式系列 (八) 】组合模式
c++·后端·设计模式·组合模式·c/c++
geovindu2 天前
rust: Composite Pattern
开发语言·后端·设计模式·rust·组合模式
YYYing.2 天前
【设计模式系列 (七) 】桥接模式
c++·后端·设计模式·桥接模式
阿狗童鞋3 天前
设计模式实战指南
设计模式
电子科技圈3 天前
基于车载SerDes和OpenGMSL协议的SoC设计与验证:行业高频问题解析
设计模式·车载系统·软件构建·设计规范
ClinicTech3 天前
高速冷冻离心机的温控系统与安全保护逻辑解析
设计模式·架构·健康医疗·设计语言
GISMagic3 天前
3.《设计模式》:软件设计与解耦能力提炼
设计模式·ai coding
染指11103 天前
131.Agent-Agent设计模式-MAS多智能体系统(Multi-Agent-System)
人工智能·设计模式·langchain·agent·agents