Memento Pattern 备忘录模式
简述
- 备忘录模式在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
- 可以这里理解备忘录模式:现实生活中的备忘录是用来记录某些要去做的事情,或者是记录已经达成的共同的意见的事情,以防忘记。在软件层面,备忘录模式有着相同的意义,备忘录对象主要用来记录一个对象的某种状态,或者某些数据,当要做回退时,可以从备忘录对象里获取原来的数据进行恢复操作。
- 备忘录模式属于行为型模式。
角色
- Originator 对象,需要保存状态的。
- Memento 备忘录对象,负责保存好记录,即Originator的内部状态。
- Caretaker:守护者对象,负责保存多个备忘录对象,使用集合管理,提高效率
- 说明:如果希望保存多个originator对象的不同时间的状态,也可以只需要一个HashMap< String,集合>
代码:
public class Originator {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
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 {
private List<Memento> mementoList=new ArrayList<>();
public void add(Memento memento){
mementoList.add(memento);
}
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("状态#1:生命值50");
caretaker.add(originator.saveStateMemento());
System.out.println("当前状态为:"+originator.getState());
originator.getStateFromMemento(caretaker.get(0));//得到状态#1
System.out.println("恢复,当前的状态为:"+originator.getState());
}
}
例子
游戏角色恢复:Memento,两个属性,攻击力vit与防御力def。Caretaker类,有Memento属性(保存一次就不用集合了)。GameRole,有createMemento方法(返回Memento);有recover方法,恢复到备忘录;有display方法打印状态。
Memento类:
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;
}
}
Caretaker类:
public class Caretaker {
private Memento memento;
//对GameRole保持多次状态
// private ArrayList<Memento> mementos;
//对多个GameRole保存多次状态
// private HashMap<String,ArrayList<Memento>> rolesMementos;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
GameRole类:
public class GameRole {
private int vit;
private int def;
public GameRole(int vit, int def) {
this.vit = vit;
this.def = def;
}
public void setVit(int vit) {
this.vit = vit;
}
public void setDef(int def) {
this.def = def;
}
public Memento createMemento(){
return new Memento(vit,def);
}
public void recoverFromMemento(Memento memento){
vit=memento.getVit();
def=memento.getDef();
}
public void display(){
System.out.println("角色当前状态:攻击力【"+vit+"】,防御力【"+def+"】。");
}
}
Client主方法
public class Client {
public static void main(String[] args) {
GameRole gameRole=new GameRole(100,100);
System.out.println("大战前状态:");
gameRole.display();
Caretaker caretaker=new Caretaker();
caretaker.setMemento(gameRole.createMemento());
System.out.println("大战进行了十分钟:");
gameRole.setVit(70);
gameRole.setDef(60);
gameRole.display();
System.out.println("大战后恢复:");
gameRole.recoverFromMemento(caretaker.getMemento());
gameRole.display();
}
}
结果:
注意事项
- 给用户提供一种可以恢复状态的机制,可以使用户能够比较方便的回到某个历史的状态。
- 实现了信息的封装,使得用户不需要关心状态的保存细节。
- 如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存,这个需要注意。
- 适用的场景:1、后悔药。2、打游戏时的存档。3、windows里的ctrl+z。4、IE的后退。5、数据库的事务管理。
- 为了节约内存,备忘录模式可以和原型模式配合使用。