备忘录模式
在不破坏封装性的前提下,补货一个对象的内部状态,并在改对象之外保存这个状态,这样就可以将现在的状态恢复到原先保存的状态。
模型图
代码
public class Originator {
private String state="on";
private Memento memento=null;
public Memento getMemento() {
return new Memento(state);
}
public void setMemento(Memento memento) {
this.state=memento.getState();
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void showState(){
System.out.println("状态:="+this.state);
}
}
public class Memento {
private String state="on";
public Memento(String state) {
this.state=state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
public class CareTaker {
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class Test {
public static void main(String[] args) {
Originator originator=new Originator();
originator.setState("on");
originator.showState();
CareTaker careTaker=new CareTaker();
careTaker.setMemento(originator.getMemento());
originator.setState("off");
originator.showState();
originator.setMemento(careTaker.getMemento());
originator.showState();
}
}
案例 模拟游戏角色生命存档
分析设计 通过备忘录模式进行存档
模型图
public class RoleGame {
//生命力
private int live;
//法力
private int atk;
//攻击力
private int hit;
private RoleStateMemento roleStateMemento=null;
public RoleStateMemento getRoleStateMemento() {
return new RoleStateMemento(live,atk,hit);
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.live=roleStateMemento.getLive();
this.atk=roleStateMemento.getAtk();
this.hit=roleStateMemento.getHit();
}
public int getLive() {
return live;
}
public void setLive(int live) {
this.live = live;
}
@Override
public String toString() {
return "RoleGame [live=" + live + ", atk=" + atk + ", hit=" + hit+"]";
}
public int getAtk() {
return atk;
}
public RoleGame(int live, int atk, int hit) {
super();
this.live = live;
this.atk = atk;
this.hit = hit;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
public void show(){
System.out.println(toString());
}
}
public class RoleGameCareTaker {
Map<String,RoleStateMemento> memen=new ConcurrentHashMap<String, RoleStateMemento>();
public void savePro(String key,RoleStateMemento value){
memen.put(key, value);
}
public RoleStateMemento readPro(String key){
return memen.get(key);
}
}
public class RoleStateMemento {
public RoleStateMemento(int live, int atk, int hit) {
super();
this.live = live;
this.atk = atk;
this.hit = hit;
}
//生命力
private int live;
//法力
private int atk;
//攻击力
private int hit;
public int getLive() {
return live;
}
public void setLive(int live) {
this.live = live;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
}
public class Test {
public static void main(String[] args) {
RoleGame game=new RoleGame(1, 1, 1);
game.show();
RoleGameCareTaker careTaker=new RoleGameCareTaker();
careTaker.savePro("1",game.getRoleStateMemento());
game=new RoleGame(2, 2, 2);
careTaker.savePro("2", game.getRoleStateMemento());
game.show();
game=new RoleGame(3, 3, 3);
careTaker.savePro("3", game.getRoleStateMemento());
game.show();
game.setRoleStateMemento(careTaker.readPro("1"));
game.show();
game.setRoleStateMemento(careTaker.readPro("2"));
game.show();
}
}
结果图
小结
1 将对象状态的细节保存在备忘录中 哪天更改备忘录也不至于影响到原对象。
2 适合功能比较复杂 又要保存历史属性的具体类
3 就是追溯历史变量的时候