备忘录模式(java实现)

时间:2022-07-02 15:30:57

备忘录模式

 

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 stateShow() {

System.out.println("当前角色状态为:");

System.out.println("体力:"+this.vitality);

System.out.println("攻击力:"+this.attack);

System.out.println("防御力:"+this.defense);

}

//获得初始状态

public void getInitState() {

this.vitality=100;

this.attack=100;

this.defense=100;

}

//战斗

public void fight() {

this.vitality=0;

this.attack=0;

this.defense=0;

}

public static void main(String[]args) {

GameRole role=new GameRole();

//大战boss前,获得角色初始状态

role.getInitState();

role.stateShow();

//通过构造一个新的对象来保存游戏进度

GameRole save=new GameRole();

save.vitality=role.vitality;

save.attack=role.attack;

save.defense=role.defense;

//大战boss,数据损耗为0

role.fight();

role.stateShow();

//恢复之前的状态

role.vitality=save.vitality;

role.attack=save.attack;

role.defense=save.defense;

role.stateShow();

}

}

这样写,客户端的职责太大,需要知道游戏的生命力,攻击力和防御力这些细节,还要对它进行备份和恢复,我们希望把这些游戏的存取状态细节封装起来,而且最好是封装在外部的类中,以体现职责分离.

备忘录模式:

在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后可以将该对象恢复到原先保存的状态.

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 stateShow() {

System.out.println("当前角色状态为:");

System.out.println("体力:"+this.vitality);

System.out.println("攻击力:"+this.attack);

System.out.println("防御力:"+this.defense);

}

//获得初始状态

public void getInitState() {

this.vitality=100;

this.attack=100;

this.defense=100;

}

//战斗

public void fight() {

this.vitality=0;

this.attack=0;

this.defense=0;

}

public RoleStateMemento saveState() {

return new RoleStateMemento(vitality,attack,defense);

}

public void recoverState(RoleStateMemento memento) {

this.vitality=memento.getVitality();

this.attack=memento.getAttack();

this.defense=memento.getDefense();

}

}

//角色状态存储箱

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;

}

}

public class Demo {

public static void main(String[]args) {

//大战boss前

GameRole role=new GameRole();

role.getInitState();

role.stateShow();

//保存进度,由于封装在RoleStateMemento类中

//因此我们也不知道保存了哪些具体的角色数据

RoleStateCaretaker stateAdmin=new RoleStateCaretaker();

stateAdmin.memento=role.saveState();

//大战boss

role.fight();

role.stateShow();

//恢复之前的状态

role.recoverState(stateAdmin.memento);

role.stateShow();

}

}

问题描述:备忘录模式也是有缺点的,角色状态需要完整存储到角色对象中,如果状态数据很大很多,那么在资源消耗上,备忘录对象会非常耗内存.