备忘录模式(Memento)
定义
在不破坏对象封装的前提下,捕获一个的对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
UML
对象说明
Originator:负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可以使用备忘录恢复到内部状态。Originator
可以根据需要决定Memento存储Originator
的哪些内部状态。
Memento:备忘录,负责存储Originator
对象的内部状态,并可以防止Originator
以外的其他对象访问备忘录。备忘录有两个接口,CareTaker
只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originaator
可以看到一个宽接口,允许它访问返回到先前状态所需的所有数据。
Caretaker:管理者,负责保存好备忘录,不能对备忘录的内容进行操作或者检查。
Demo
本demo主要是基于游戏进度保存的实例,将游戏角色恢复到指定的状态。 本实例主要是包含四个类组成,分别是GameRole
、RoleStateMemento
、RoleStateCaretaker
、Client
组成。
描述游戏角色的类
public class GameRole {
/**
* 描述游戏角色相关属性
* */
private int vit;
private int attack;
private int defence;
/**
* 初始化角色属性
* */
public GameRole(int vit, int attack, int defence) {
this.vit = vit;
this.attack = attack;
this.defence = defence;
}
/**
* 设置生命值
* */
public void setVit(int vit) {
this.vit = vit;
}
/**
* 获取生命值
* */
public int getVit() {
return vit;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefence() {
return defence;
}
public void setDefence(int defence) {
this.defence = defence;
}
/**
* 展示自己的状态
* */
public void show(){
System.out.println("生命值:"+vit);
System.out.println("攻击力:"+attack);
System.out.println("防御力:"+defence);
}
/**
* Fight:在与boss战斗之后,人物die
* */
public void fight(){
this.vit=0;
this.attack=0;
this.defence=0;
}
/**
* 保存游戏状态
* */
public RoleStateMemento saveState(){
return new RoleStateMemento(vit,attack,defence);
}
/**
* 恢复角色状态
* */
public void recoveryState(RoleStateMemento memento){
this.vit=memento.getVit();
this.attack=memento.getAttack();
this.defence=memento.getDefence();
}
}
memento:
public class RoleStateMemento {
private int vit;
private int attack;
private int defence;
/**
* 将生命力、攻击力、防御力存入状态村粗箱对象中
* */
public RoleStateMemento(int vit, int attack, int defence) {
this.vit = vit;
this.attack = attack;
this.defence = defence;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefence() {
return defence;
}
public void setDefence(int defence) {
this.defence = defence;
}
}
游戏进度条管理者
public class RoleStateCaretaker {
private RoleStateMemento roleStateMemento;
/**
* 获取备忘录中的信息
* */
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
/**
* 设置备忘录中的信息
* */
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
客户端
public class Client {
public static void main(String[] args) {
//大战BOSS之前
GameRole gameRole=new GameRole(100,100,100);
gameRole.show();
//保存进度
gameRole.setVit(75);
RoleStateCaretaker stateAdmin=new RoleStateCaretaker();
stateAdmin.setRoleStateMemento(gameRole.saveState());
//大战BOSS后,die
gameRole.fight();
gameRole.show();
//恢复角色状态
gameRole.recoveryState(stateAdmin.getRoleStateMemento());
gameRole.show();
}
}
小结
通过这篇讲解,可以深刻的理解备忘录模式,备忘录模式比较适合功能比较复杂的,但是需要维护或者记录属性历史的类,或者是需要保存的属性只是众多属性中的一小部分的时候,Originator
可以根据Memento
信息还原到前一状态。
如果在某个系统中使用命令模式的时候,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。
最重要的一点是:当角色的状态改变时,有可能这个状态无效,这个时候就是可以使用暂时存储起来的备忘录将状态复原。