设计模式GOF23--备忘录模式

场景

  • 录入大批人员资料。正在录入当前人资料时,发现上一个人录错了。此时需要恢复上一个人的资料,在进行修改。
  • Word文档编辑时,忽然电脑死机或者断电,再次打开时,可以恢复到之前的模式。

核心

  • 就是保存某个对象内部状态的拷贝,这样以后就可以将该对象恢复至原先的状态

结构

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

代码解析

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/**
* 源发器类
*/
public class Emp {
private String ename;
private int age;
private double salary;

//进行备忘操作,并返回备忘录对象
public EmpMemento memento(){
return new EmpMemento(this);
}


//数据恢复,恢复成指定备忘录值
public void recovery(EmpMemento memento){
this.ename = memento.getEname();
this.age = memento.getAge();
this.salary = memento.getSalary();

}
public Emp(String ename, int age, double salary) {
this.ename = ename;
this.age = age;
this.salary = salary;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

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;
}
}


/**
* 备忘录类
*/
public class EmpMemento {
private String ename;
private int age;
private double salary;

public EmpMemento(Emp e) {
this.ename = e.getEname();
this.age = e.getAge();
this.salary = e.getSalary();
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

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;
}
}
/**
* 负责人类
* 负责管理备忘录对象
*/
public class CareTaker {
private EmpMemento memento;

//此处可以存放list,保存多个点
//private List<EmpMemento> list = new ArrayList<>();


public EmpMemento getMemento() {
return memento;
}

public void setMemento(EmpMemento memento) {
this.memento = memento;
}
}
/**
* 备忘录模式
*/
public class Client {
public static void main(String[] args) {
CareTaker taker = new CareTaker();

Emp emp = new Emp("kid",18,900);

System.out.println("第一次打印对象:"+emp.getEname()+"===="+emp.getAge()+"==="+emp.getSalary());

//备忘一次
taker.setMemento(emp.memento());

emp.setAge(28);
emp.setEname("六六");
emp.setSalary(2910);

System.out.println("第二次打印对象:"+emp.getEname()+"===="+emp.getAge()+"==="+emp.getSalary());

//恢复到备忘录对象保存的状态
emp.recovery(taker.getMemento());
System.out.println("第三次打印对象:"+emp.getEname()+"===="+emp.getAge()+"==="+emp.getSalary());


}
}
结果:
第一次打印对象:kid====18===900.0
第二次打印对象:六六====28===2910.0
第三次打印对象:kid====18===900.0

备忘点较多时:

  • 将备忘录存为List
  • 将备忘录压栈
  • 将多个备忘录对象,序列化和持久化

开发中常见的应用场景

  • 棋类游戏中,悔棋
  • 普通软件中的,撤销操作
  • 数据库软件中,事务管理中,回滚操作

    注:该博文为学习总结,视频来源为高淇java300集