白箱备忘录模式
package per.mjn.pattern.memento.white_box;
// 游戏角色类(属于发起人角色)
public class GameRole {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
// 初始化内部状态
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
// 战斗
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
// 保存角色状态功能
public RoleStateMemento saveState() {
return new RoleStateMemento(vit, atk, def);
}
// 恢复角色状态
public void recoverState(RoleStateMemento roleStateMemento) {
// 将备忘录对象存储的状态赋值给当前对象的成员
this.vit = roleStateMemento.getVit();
this.atk = roleStateMemento.getAtk();
this.def = roleStateMemento.getDef();
}
// 展示状态功能
public void stateDisplay() {
System.out.println("角色生命力: " + vit);
System.out.println("角色攻击力: " + atk);
System.out.println("角色防御力: " + def);
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
package per.mjn.pattern.memento.white_box;
// 备忘录角色类
public class RoleStateMemento {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
public RoleStateMemento() {
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
package per.mjn.pattern.memento.white_box;
// 备忘录对象管理对象
public class RoleStateCaretaker {
// 声明RoleStateMemento类型的变量
private RoleStateMemento roleStateMemento;
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
package per.mjn.pattern.memento.white_box;
public class Client {
public static void main(String[] args) {
System.out.println("---------大战BOSS前-------------");
// 创建游戏角色对象
GameRole gameRole = new GameRole();
gameRole.initState(); // 初始状态操作
gameRole.stateDisplay();
// 将该游戏角色内部状态进行备份
// 创建管理者对象
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setRoleStateMemento(gameRole.saveState());
System.out.println("---------大战BOSS后-------------");
// 损耗严重
gameRole.fight();
gameRole.stateDisplay();
System.out.println("---------恢复战前的状态-------------");
gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
gameRole.stateDisplay();
}
}
黑箱备忘录模式
备忘录角色对发起人对象提供一个宽接口,而为其他对象提供一个窄接口。在Java语言中,实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类。
package per.mjn.pattern.memento.black_box;
// 备忘录接口,对外提供窄接口
public interface Memento {
}
package per.mjn.pattern.memento.black_box;
// 游戏角色类(属于发起人角色)
public class GameRole {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
// 初始化内部状态
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
// 战斗
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
// 保存角色状态功能
public Memento saveState() {
return new RoleStateMemento(vit, atk, def);
}
// 恢复角色状态
public void recoverState(Memento memento) {
RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
// 将备忘录对象存储的状态赋值给当前对象的成员
this.vit = roleStateMemento.getVit();
this.atk = roleStateMemento.getAtk();
this.def = roleStateMemento.getDef();
}
// 展示状态功能
public void stateDisplay() {
System.out.println("角色生命力: " + vit);
System.out.println("角色攻击力: " + atk);
System.out.println("角色防御力: " + def);
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
// 黑箱备忘录
private class RoleStateMemento implements Memento {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
public RoleStateMemento() {
}
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
}
package per.mjn.pattern.memento.black_box;
import per.mjn.pattern.memento.white_box.RoleStateMemento;
// 备忘录对象管理对象
public class RoleStateCaretaker {
// 声明RoleStateMemento类型的变量
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
package per.mjn.pattern.memento.black_box;
public class Client {
public static void main(String[] args) {
System.out.println("---------大战BOSS前-------------");
// 创建游戏角色对象
GameRole gameRole = new GameRole();
gameRole.initState(); // 初始状态操作
gameRole.stateDisplay();
// 将该游戏角色内部状态进行备份
// 创建管理者对象
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setMemento(gameRole.saveState());
System.out.println("---------大战BOSS后-------------");
// 损耗严重
gameRole.fight();
gameRole.stateDisplay();
System.out.println("---------恢复战前的状态-------------");
gameRole.recoverState(roleStateCaretaker.getMemento());
gameRole.stateDisplay();
}
}
备忘录有两个等效的接口:
窄接口:管理者(Caretaker)对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口(narror Interface),这个窄接口只允许他把备忘录对象传给其他的对象。
宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口(wide Interface),这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。