备忘录模式
备忘录模式是什么?
在不破坏封装性的前提下,捕获一个对的的内部状态,并在该对象之外保持这个状态。这样以后就可将该对象恢复到原先保持的状态
备忘录模式解决什么问题?
如游戏打Boss失败时回档,定义一个游戏角色
public class GameRole {
private int vitality;
private int attack;
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public void displayState() {
System.out.println("角色当前状态:");
System.out.println("体力:" + vitality);
System.out.println("攻击力:" + attack);
System.out.println("防御力:" + defense);
}
public void getInitState() {
vitality = 100;
attack = 100;
defense = 100;
}
public void fight() {
vitality = 0;
attack = 0;
defense = 0;
}
}
在挑战Boss前,复制一份当前的状态
GameRole role = new GameRole();
role.getInitState();
role.displayState();
GameRole backup = new GameRole();
backup.setVitality(role.getVitality());
backup.setAttack(role.getAttack());
backup.setDefense(role.getDefense());
当挑战Boss失败后,通过此前的复制还原状态
role.fight();
role.displayState();
role.setVitality(backup.getVitality());
role.setAttack(backup.getAttack());
role.setDefense(backup.getDefense());
role.displayState();
- problem1:需要对属性一个个进行备份
- problem2:当属性增加或修改时,需要对代码修改
备忘录模式实现
创建备忘录,添加要保存的数据
public class RoleStateMemento {
private int vitality;
private int attack;
private int defense;
public RoleStateMemento(int vitality, int attack, int defense) {
this.vitality = vitality;
this.attack = attack;
this.defense = defense;
}
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
}
修改GameRole类,添加备份/还原方法
public class GameRole {
private int vitality;
private int attack;
private int defense;
public int getVitality() {
return vitality;
}
public void setVitality(int vitality) {
this.vitality = vitality;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefense() {
return defense;
}
public void setDefense(int defense) {
this.defense = defense;
}
public void displayState() {
System.out.println("角色当前状态:");
System.out.println("体力:" + vitality);
System.out.println("攻击力:" + attack);
System.out.println("防御力:" + defense);
}
public void getInitState() {
vitality = 100;
attack = 100;
defense = 100;
}
public void fight() {
vitality = 0;
attack = 0;
defense = 0;
}
public RoleStateMemento saveState() {
return new RoleStateMemento(vitality, attack, defense);
}
public void recoveryState(RoleStateMemento memento) {
vitality = memento.getVitality();
attack = memento.getAttack();
defense = memento.getDefense();
}
}
创建它们之间的管理类
public class RoleStateCaretaker {
private RoleStateMemento mMemento;
public RoleStateMemento getMemento() {
return mMemento;
}
public void setMemento(RoleStateMemento memento) {
mMemento = memento;
}
}
备份/还原代码如下
GameRole role = new GameRole();
role.getInitState();
role.displayState();
RoleStateCaretaker stateCaretaker = new RoleStateCaretaker();
stateCaretaker.setMemento(role.saveState());
role.fight();
role.displayState();
role.recoveryState(stateCaretaker.getMemento());
role.displayState();
本文转载自: https://blog.csdn.net/qq_35258036/article/details/132992972
版权归原作者 松仔log 所有, 如有侵权,请联系我们删除。
版权归原作者 松仔log 所有, 如有侵权,请联系我们删除。