定义
又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。
结构
- 发起人角色(Originator):记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能。它可以访问备忘录里的所有信息。
- 备忘录角色(Memento):负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人
- 管理者角色(Caretaker):对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
备忘录有两个等效的接口:
- 窄接口:管理者对象以及其他非发起人对象看到的事被忘了的窄接口,这个窄接口只允许他把备忘录对象传给其他的对象
- 宽接口:与窄接口相反,发起人对象可以看到一个宽接口,这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。
案例
游戏中的某个角色挑战boss,挑战前的基础数据和挑战后的基础数据不一样,比如血量降低了,防御力降低了等。挑战后,允许玩家恢复到挑战之前的状态。
有两种方式:
- “白箱”备忘录模式
- “黑箱”备忘录模式
白箱备忘录模式
备忘录角色对任何对象都提供一个接口,及宽接口。备忘录角色的内部所存储的状态就对所有对象公开。
备忘录角色
package com.hupp.memento.white_box;
/**
* 备忘录角色
*/
public class RoleStateMemento {
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 com.hupp.memento.white_box;
/**
* 备忘录管理对象
*/
public class RoleStateCaretaker {
//声明备忘录对象
private RoleStateMemento roleStateMemento;
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
发起人角色
package com.hupp.memento.white_box;
/**
* 游戏角色(发起人角色)
*/
public class GameRole {
private int vit;//生命力
private int atk;//攻击力
private int def;//防御力
//初始化内部状态
public void initState() {
vit = 100;
atk = 100;
def = 100;
}
//战斗
public void fight() {
vit = vit > 20 ? vit - 20 : 0;
atk = atk > 10 ? atk - 10 : 0;
def = def > 8 ? def - 8 : 0;
}
//保存状态
public RoleStateMemento saveState() {
return new RoleStateMemento(vit, atk, def);
}
//恢复角色状态
public void recoverState(RoleStateMemento stateMemento) {
this.vit = stateMemento.getVit();
this.atk = stateMemento.getAtk();
this.def = stateMemento.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 com.hupp.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("-----------恢复到挑战boss前------------");
gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
gameRole.stateDisplay();
}
}
黑箱备忘录模式
备忘录角色对发起人提供一个宽接口,而为其他对象提供一个窄接口。在Java中,实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类。
备忘录接口,没有任何方法
package com.hupp.memento.black_box;
/**
* 备忘录接口,对外提供窄接口
*/
public interface Memento {
}
发起人角色和备忘录角色,备忘录角色是发起人类的私有内部类
package com.hupp.memento.black_box;
/**
* 游戏角色(发起人角色)
*/
public class GameRole {
private int vit;//生命力
private int atk;//攻击力
private int def;//防御力
//初始化内部状态
public void initState() {
vit = 100;
atk = 100;
def = 100;
}
//战斗
public void fight() {
vit = vit > 20 ? vit - 20 : 0;
atk = atk > 10 ? atk - 10 : 0;
def = def > 8 ? def - 8 : 0;
}
//保存状态
public Memento saveState() {
return new RoleStateMemento(vit, atk, def);
}
//恢复角色状态
public void recoverState(Memento memento) {
RoleStateMemento stateMemento = (RoleStateMemento) memento;
this.vit = stateMemento.getVit();
this.atk = stateMemento.getAtk();
this.def = stateMemento.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 com.hupp.memento.black_box;
/**
* 备忘录管理对象
*/
public class RoleStateCaretaker {
//声明备忘录对象
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
客户端
package com.hupp.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("-----------恢复到挑战boss前------------");
gameRole.recoverState(roleStateCaretaker.getMemento());
gameRole.stateDisplay();
}
}