备忘录模式

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图

相关推荐
2401_8685347813 小时前
网规备考_2.4 路由协议
c++·设计模式
cpp_learner14 小时前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
c++·设计模式
码匠许师傅15 小时前
【设计模式精讲】23.备忘录模式(Memento)
c++·设计模式·软件工程·uml·备忘录模式
新知图书1 天前
第8章 多智能体协同
人工智能·设计模式·智能体
京师20万禁军教头1 天前
39面向对象(高级)-设计模式
java·开发语言·设计模式
新知图书2 天前
3.5 智能体设计模式1:反应式智能体与自动导航案例
人工智能·设计模式·智能体
码匠许师傅2 天前
【设计模式精讲】21.迭代器模式(Iterator)
c++·设计模式·rpc·迭代器模式·软件工程·uml
Carl_奕然3 天前
【智能体】Agent的四种设计模式之:React(2026最新版)
javascript·人工智能·python·react.js·设计模式·语言模型
cfm_29143 天前
Spring核心设计模式
java·spring·设计模式
码匠许师傅4 天前
【设计模式精讲】15.享元模式(Flyweight)
c++·设计模式·享元模式·uml