备忘录模式
原理类图:
角色分析:
1) originator : 对象(需要保存状态的对象)
2) Memento : 备忘录对象,负责保存好记录,即Originator内部状态
3) Caretaker: 守护者对象,负责保存多个备忘录对象, 使用集合管理,提高效率 它只负责存储对象,而不能修改对象,也无须知道对象的实现细节。
4) 说明:如果希望保存多个originator对象的不同时间的状态,也可以,只需要 HashMap <String, 集合>
为什么会出现守护者对象?
举个例子说明,下棋软件要提供“悔棋”功能,用户走错棋或操作失误后可恢复到前一个步骤。悔棋可能回到上一步,也有可能回到上上次的状态…因此需要记录多次的状态
在设计备忘录类时需要考虑其封装性,除了Originator类,不允许其他类来调用备忘录类Memento的构造函数与相关方法,如果不考虑封装性,允许其他类调用setState()等方法,将导致在备忘录中保存的历史状态发生改变,通过撤销操作所恢复的状态就不再是真实的历史状态,备忘录模式也就失去了本身的意义。
备忘录模式实现框架:
Memento : 备忘录对象,负责保存好记录,即Originator内部状态
/**
* @author 孙一鸣 on 2020/2/20
*/
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
originator : 对象(需要保存状态的对象)
/**
* @author 孙一鸣 on 2020/2/20
*/
public class Originator {
private String state;//状态
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
//编写一个方法,可以保存一个状态对象Memento
public Memento saveStateMemento(){
return new Memento(state);
}
public void getStateFromMemento (Memento memento){
state = memento.getState();
}
}
Caretaker: 守护者对象,负责保存多个备忘录对象, 使用集合管理,提高效率
/**
* @author 孙一鸣 on 2020/2/20
*/
public class Caretaker {
//List集合中会有很多备忘录对象
private List<Memento> mementoList = new ArrayList<>();
public void add(Memento memento) {
mementoList.add(memento);
}
//获取到第index个Origintor的备忘录对象
public Memento get(int index){
return mementoList.get(index);
}
}
游戏角色恢复案例:
游戏角色有攻击力和防御力,在大战Boss前保存自身的状态(攻击力和防御力),当大战
Boss后攻击力和防御力下降,从备忘录对象恢复到大战前的状态
游戏角色 (需要存档):
/**
* 游戏角色
*
* @author 孙一鸣 on 2020/2/20
*/
public class GameRole {
private int vit;
private int def;
//根据当前状态创建Memento,
public Memento createMemento() {
return new Memento(vit, def);
}
//从备忘录中找到Memento对象,恢复状态
public void recoverGameRoleFromMemento(Memento memento) {
this.vit = memento.getVit();
this.def = memento.getDef();
}
//显示当前游戏角色状态
public void display (){
System.out.println("游戏角色当前状态:"+this.vit +this.def);
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
备忘录对象:存储游戏角色状态
/**
* 备忘录对象
*
* @author 孙一鸣 on 2020/2/20
*/
public class Memento {
//攻击力
private int vit;
//防御力
private int def;
public Memento(int vit, int def) {
this.vit = vit;
this.def = def;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
守护者对象,保存游戏角色状态
/**
* 守护者对象,保存游戏角色状态
* @author 孙一鸣 on 2020/2/20
*/
public class Caretaker {
//如果只保存一次状态
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
测试:
/**
* @author 孙一鸣 on 2020/2/20
*/
public class Client {
public static void main(String[] args) {
//创建游戏角色
GameRole gameRole = new GameRole();
gameRole.setDef(100);
gameRole.setVit(100);
System.out.println("大战前的状态");
gameRole.display();
//保存状态
Caretaker caretaker = new Caretaker();
caretaker.setMemento(gameRole.createMemento());
System.out.println("和Boss大战");
gameRole.setDef(20);
gameRole.setVit(20);
gameRole.display();
System.out.println("大战后 使用备忘录恢复");
gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
System.out.println("回复后的状态:");
gameRole.display();
}
}
结果截图:
备忘录模式的注意事项和细节
- 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态
- 实现了信息的封装,使得用户不需要关心状态的保存细节
- 如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存, 这个需要注意
- 适用的应用场景:1、后悔药。 2、打游戏时的存档。 3、Windows 里的 ctri + z。4、IE 中的后退。 4、数据库的事务管理
- 为了节约内存,备忘录模式可以和原型模式配合使用