备忘录模式

1、游戏角色状态恢复问题

游戏角色有攻击力和防御力、生命力,在大战Boss之前保存自身的状态(攻击力)和(防御力)、生命力,当大战Boss后攻击力和防御力、生命力下降,从备忘录对象恢复到大战前的状态。

【传统设计方案】

对象中保存对象的游戏状态,这样的话每个角色都要单独保存状态。

【传统方式的问题分析】

1)一个对象,就对应一个保存对象状态的对象,这样当我们游戏的对象很多时,不利于管理,开销也很大。

2)传统的方式是简单地做备份,new出另外一个对象出来,再把需要备份的数据放到这个新对象,但这就暴露了对象内部的细节。

3)解决方案:备忘录模式


2、备忘录模式的基本介绍

1)备忘录模式(Memento Pattern)在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态。

2)可以这样理解备忘录模式:现实生活中的备忘录是用来记录某些要去做的事情,或者是记录已经达成的共同意见的事情,以防忘记了。而在软件层面,备忘录模式有着相同的含义,备忘录对象主要用来记录一个对象的某种状态,或者某些数据,当要做回退时,可以从备忘录对象里获取原来的数据进行恢复操作。

3)备忘录模式属于行为型模式。


3、结构

1)发起人(Originator)角色:记录当前时刻的内部状态信息,即需要保存状态的原始对象。提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。

2)备忘录(Memento)角色:负责存储发起人的内部状态,即Originator内部状态。在需要的时候提供这些内部状态给发起人。

3)管理者(Caretaker)角色:对备忘录进行管理,负责保存多个备忘录对象,使用集合管理,提高效率。提供保存于获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

4)说明:如果希望保存多个originator对象的不同时间的状态,也可以,只需要HashMap<String, 集合>

备忘录有两个等效的接口:

  • 窄接口:管理者(Caretaker)对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口(narror Interface),这个窄接口只允许他把备忘录对象传给其他的对象。
  • 宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口(wide Interface),这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。

4、应用实例代码实现

【1】”白箱”备忘录模式

备忘录角色对任何对象都提供一个接口,即宽接口,备忘录角色的内部所存储的状态就对所有对象公开。类图如下:

image-20230716130605948

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
// 游戏角色类(属于发起人角色)
public class GameRole {
private int vit; //生命力
private int atk; //攻击力
private int def; //防御力

public GameRole() {
}
public GameRole(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
getter、setter

// 初始化内部状态
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}

// 战斗
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}

// 创建Memento,即根据当前的状态得到Memento
public Memento createMemento() {
return new Memento(vit, atk, def);
}

// 从备忘录对象恢复状态
public void recoverFromMemento(Memento memento) {
this.vit = memento.getVit();
this.atk = memento.getAtk();
this.def = memento.Def();
}

// 显示当前游戏角色的状态
public void displayState() {
System.out.println("游戏角色当前的攻击力:" + atk + " 防御力:" + defend, "生命力:" + vit);
}


}

public class Memento {
private int vit; //生命力
private int atk; //攻击力
private int def; //防御力

public GameRole() {
}
public GameRole(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
getter、setter
}

// 备忘录管理对象,保存游戏角色的状态
public class Caretaker {
// 如果只保存一次状态
private Memento memento;
// 对GameRole保存多次状态
//private ArrayList<Memento> mementos;
// 对多个游戏角色保存多个状态
//private HashMap<String, ArrayList<Memento>> rolesMementos;

public void setMemento(Memento memento) {
this.memento = memento;
}

public Memento getMemento() {
return memento;
}
}

public class Client {
public static void main(String[] args) {
GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.diaplayState();

// 创建管理者对象
Caretaker caretaker = new Caretaker();
caretaker.setMemento(gameRole.createMemento());

System.out.println("和boss大战~~~");
gameRole.setVit(30);
gameRole.setAtk(30);
gameRole.setDef(30);
gameRole.displayState();

System.out.println("大战后,使用备忘录对象恢复到大战前");
gameRole.recoverFromMemento(caretaker.getMemento());
gameRole.displayState();

}
}

【2】”黑箱”备忘录模式

分析:白箱备忘录模式是破坏封装性的。但是通过程序员自律,同样可以在一定程度上实现模式的大部分用意。

备忘录角色对发起人对象提供一个宽接口,而为其他对象提供一个窄接口。在Java语言中,实现双重接口的办法就是将备忘录类设计成发起人类的成员内部类。

RoleStateMemento设为GameRole的内部类,从而将RoleStateMemento对象封装在GameRole里面;在外面提供一个标识接口MementoRoleStateCaretaker及其他对象使用。这样GameRole类看到的是RoleStateMemento所有的接口,而RoleStateCaretaker及其他对象看到的仅仅是标识接口Memento所暴露出来的接口,从而维护了封装性。

image-20230716134608212

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
// 备忘录接口,对外提供窄接口
public interface Memento {

}

public class GameRole {
private int vit; //生命力
private int atk; //攻击力
private int def; //防御力

public GameRole() {
}
public GameRole(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
getter、setter

// 初始化内部状态
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}

// 战斗
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}

// 创建Memento,即根据当前的状态得到Memento
public Memento createMemento() {
return new RoleStateMemento(vit, atk, def);
}

// 从备忘录对象恢复状态
public void recoverFromMemento(Memento memento) {
RoleStateMemento roleStateMemento = (RoleStateMemento)memento
this.vit = roleStateMemento.getVit();
this.atk = roleStateMemento.getAtk();
this.def = roleStateMemento.Def();
}

// 显示当前游戏角色的状态
public void displayState() {
System.out.println("游戏角色当前的攻击力:" + atk + " 防御力:" + defend, "生命力:" + vit);
}


private class RoleStateMemento implements Memento {
private int vit; //生命力
private int atk; //攻击力
private int def; //防御力

public GameRole() {
}
public GameRole(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
getter、setter
}
}



// 守护者对象,保存游戏角色的状态
public class Caretaker {
// 如果只保存一次状态
private Memento memento;
// 对GameRole保存多次状态
//private ArrayList<Memento> mementos;
// 对多个游戏角色保存多个状态
//private HashMap<String, ArrayList<Memento>> rolesMementos;

public void setMemento(Memento memento) {
this.memento = memento;
}

public Memento getMemento() {
return memento;
}
}

public class Client {
public static void main(String[] args) {

GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.diaplayState();

// 创建管理者对象
Caretaker caretaker = new Caretaker();
caretaker.setMemento(gameRole.createMemento());

System.out.println("和boss大战~~~");
gameRole.setVit(30);
gameRole.setAtk(30);
gameRole.setDef(30);
gameRole.displayState();

System.out.println("大战后,使用备忘录对象恢复到大战前");
gameRole.recoverFromMemento(caretaker.getMemento());
gameRole.displayState();
}
}
【3】保存多个状态
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
public class Originator {
private String state; // 状态信息

public String getState() {
return state;
}

// 编写一个方法,可以保存一个状态对象Memento
public Memento saveStateMemento() {
return new Memento(state);
}

// 通过备忘录对象,恢复状态
public void getStateFromMemento(Memento memento) {
state = memento.getState();
}
}

public class Memento {
private String state;

// 构造器
public Memento(String state) {
this.state = state;
}

public String getState() {
return state;
}
}

public class Caretaker {
// 在List集合中会有很多的备忘录对象
private List<Memento> mementoList = new ArrayList<Memento>();

public void add(Memento memento) {
mementoList.add(memento);
}

// 获取到第index个Originator的备忘录对象(即保存状态)
public Memento get(int index) {
return mementoList.get(index);
}
}

public class Client {
public static void main(String[] args) {
Originator originator = new Originator();
Caretaker caretaker = new Caretaker();

originator.setState(" 状态#1 攻击力100 ");
// 保存当前满血状态
caretaker.add(originator.saveStateMemento());

originator.setState(" 状态#2 攻击力80 ");
caretaker.add(originator.saveStateMemento());

originator.setState(" 状态#3 攻击力50 ");
caretaker.add(originator.saveStateMemento());

// 当前状态
System.out.println("当前状态是=" + originator.getState());

// 恢复到状态1
originator.getStateFromMenento(caretaker.get(0));
System.out.println("当前状态是=" + originator.getState());

}
}

5、备忘录模式的注意事项

【1】优点

1)给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史状态。

2)实现了内部状态的封装,除了创建它的发起人之外,其他对象都不能够访问这些状态信息。

3)简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。

【2】缺点

1)如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存,这个需要注意。

【3】适用场景

1)适用的应用场景:后悔药、打游戏时的存档、Windows里的ctrl z、IE中的后退、数据库的事务管理。

2)为了节约内存,备忘录模式可以和原型模式配合使用。