代码演示了一个单状态单备份的例子,逻辑非常简单:Originator类中的state变量需要备份,以便在需要的时候恢复;Memento类中,也有一个state变量,用来存储Originator类中state变量的临时状态;而Caretaker类就是用来管理备忘录类的,用来向备忘录对象中写入状态或者取回状态。
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
举例说明:
以保存游戏进度为例,在游戏角色大战Boss前将该角色的状态存储,与Boss作战后角色的各项能力会下降,如果没有通关,则可利用备忘录进行恢复到战前状态。
游戏角色类:
package com.zyh.designpattern.memento;
public class PlayRole {
private int vitality;
private int aggressivity;
private int defencivity;
public PlayRole(int vitality, int aggressivity, int defencivity) {
super();
this.vitality = vitality;
this.aggressivity = aggressivity;
this.defencivity = defencivity;
}
public PlayRole() {}
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAggressivity() {
return aggressivity;
}
public void setAggressivity(int aggressivity) {
this.aggressivity = aggressivity;
}
public int getDefencivity() {
return defencivity;
}
public void setDefencivity(int defencivity) {
this.defencivity = defencivity;
}
//增加“保存角色状态的方法”,将游戏角色的三个状态值通过实例化“角色状态存储箱”返回
public RoleMemento createMemento() {
RoleMemento memento = new RoleMemento();
memento.setAggressivity(aggressivity);
memento.setDefencivity(defencivity);
memento.setVitality(vitality);
return memento;
}
//新增“恢复角色状态”方法,可将外部的“角色状态存储箱”中状态值恢复给游戏角色
public void setMemento(RoleMemento memento) {
this.aggressivity = memento.getAggressivity();
this.defencivity = memento.getDefencivity();
this.vitality = memento.getVitality();
}
public void showState() {
System.out.println("攻击力:" + this.aggressivity + "|防御力:" + this.defencivity
+ "|生命力:" + this.vitality);
}
}
备忘录类:
package com.zyh.designpattern.memento;
public class RoleMemento {
private int vitality;
private int aggressivity;
private int defencivity;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAggressivity() {
return aggressivity;
}
public void setAggressivity(int aggressivity) {
this.aggressivity = aggressivity;
}
public int getDefencivity() {
return defencivity;
}
public void setDefencivity(int defencivity) {
this.defencivity = defencivity;
}
}
管理者类
package com.zyh.designpattern.memento;
public class Caretaker {
RoleMemento memento;
public RoleMemento getMemento() {
return memento;
}
public void setMemento(RoleMemento memento) {
this.memento = memento;
}
}
客户端:
package com.zyh.designpattern.memento;
public class Client {
public static void main(String[] args) {
// 测试程序
// 新建角色
PlayRole role = new PlayRole(100, 100, 100);
// 新建管理者
Caretaker taker = new Caretaker();
// 角色初始状态
System.out.println("游戏刚开始,角色各属性:");
role.showState();
// 利用备忘录模式保存当前状态
System.out.println("\n【保存游戏状态!】\n");
taker.setMemento(role.createMemento());
role.setAggressivity(20);
role.setDefencivity(30);
role.setVitality(0);
// 大战过后,角色能力值下降
System.out.println("与Boss对战后,角色各项能力已大大下降:");
role.showState();
// 恢复保存的角色状态
role.setMemento(taker.getMemento());
System.out.println("\n【恢复保存的角色状态!】");
System.out.println("\n恢复后角色的当前状态:");
role.showState();
}
}