一、概念
把要保存的细节封装在了Memento中,如果要改保存的细节也不用影响客户端。该模式比较适用于功能比较复杂但需要维护或记录属性的类,Originator可以根据保存的Memento还原到前一状态。
比如需要实现命令的撤销功能,就可以使用备忘录模式来存储可撤销操作的状态。
二、示例代码
第一版
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
public class Originator {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento createMemento() {
return new Memento(state);
}
public void recoverMemento(Memento memento) {
state = memento.getState();
}
public void show() {
System.out.println("当前状态:" + state);
}
}
public class Caretaker {
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class RunMain {
public static void main(String[] args) {
Originator originator = new Originator();
originator.setState("100");
originator.show();
Caretaker caretaker = new Caretaker();
caretaker.setMemento(originator.createMemento());
originator.setState("0");
originator.show();
originator.recoverMemento(caretaker.getMemento());
originator.show();
}
}
第二版
public class GameRoleBackUp {
private Integer hp;
private Integer mp;
public GameRoleBackUp(Integer hp, Integer mp) {
this.hp = hp;
this.mp = mp;
}
public Integer getHp() {
return hp;
}
public void setHp(Integer hp) {
this.hp = hp;
}
public Integer getMp() {
return mp;
}
public void setMp(Integer mp) {
this.mp = mp;
}
}
public class GameProcessBackUp {
// todo
}
public class GameRole {
// Health Point,直译就是健康点数的意思,俗称“红”
private Integer hp;
// Magic Point,直译魔法值的意思,俗称“蓝”
private Integer mp;
public GameRole(Integer hp, Integer mp) {
this.hp = hp;
this.mp = mp;
}
public Integer getHp() {
return hp;
}
public void setHp(Integer hp) {
this.hp = hp;
}
public Integer getMp() {
return mp;
}
public void setMp(Integer mp) {
this.mp = mp;
}
public GameRoleBackUp save() {
return new GameRoleBackUp(hp, mp);
}
public void load(GameRoleBackUp gameRoleBackUp) {
this.hp = gameRoleBackUp.getHp();
this.mp = gameRoleBackUp.getMp();
}
@Override
public String toString() {
return "GameRole{" + "hp=" + hp + ", mp=" + mp + '}';
}
}
public class BackUpManagement {
private GameRoleBackUp gameRoleBackUp;
private GameProcessBackUp gameProcessBackUp;
public GameRoleBackUp getGameRoleBackUp() {
return gameRoleBackUp;
}
public void setGameRoleBackUp(GameRoleBackUp gameRoleBackUp) {
this.gameRoleBackUp = gameRoleBackUp;
}
public GameProcessBackUp getGameProcessBackUp() {
return gameProcessBackUp;
}
public void setGameProcessBackUp(GameProcessBackUp gameProcessBackUp) {
this.gameProcessBackUp = gameProcessBackUp;
}
}
public class RunMain {
public static void main(String[] args) {
GameRole gameRole = new GameRole(100, 100);
System.out.println(gameRole);
// save
BackUpManagement backUpManagement = new BackUpManagement();
backUpManagement.setGameRoleBackUp(gameRole.save());
// fight
gameRole.setHp(0);
gameRole.setMp(0);
System.out.println(gameRole);
// load
gameRole.load(backUpManagement.getGameRoleBackUp());
System.out.println(gameRole);
}
}
三、总结
备忘录模式就是SL大法,SL真香!