游戏进度恢复--备忘录模式
- 人工智能
- 2025-07-21 18:53:27

缘起
某日,部门Leader给小明布置了一个任务:编码出游戏某个场景,游戏角色有生命力、攻击力、防御力等数据,打Boss前和后数据是不一样的,我们允许玩家若感觉与Boss决斗效果不理想可以让游戏恢复到决斗前。
小明的代码
public class GameRole { private int vitality; // 生命 private int attack; // 攻击力 private int defense; // 防御力 ...getter/setter public void displayState() { // 显示状态 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; } }客户端调用
GameRole role = new GameRole(); role.getInitState(); role.displayState(); // 保存进度 GameRole backup = new GameRole(); backup.setVitality(role.getVitality()); backup.setAttack(role.getAttack()); backup.setDefense(role.getDefense()); // 战斗 role.fight(); role.displayState(); // 游戏进度恢复 role.setVitality(backup.getVitality()); role.setAttack(backup.getAttack()); role.setDefense(backup.getDefense()); role.displayState();Leader看后:“代码无错未必优,问题主要在于客户端的调用。这样把游戏的整个角色细节都暴露给了客户端,你的客户端职责太大了,后面若添加新的属性,客户端的工作就太多了,你可以试试备忘录模式”
备忘录模式备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并且在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态
结构图
Originator(发起人):负责创建一个备忘录Memento,用于记录当前时刻的内部状态Memento(备忘录):负责存储Originator对象的内部状态Caretaker(管理者):负责保存好备忘录Memento备忘录模式基本代码
Originator发起人 public class Originator { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } // 显示数据 public void show() { System.out.println("state is " + this.state); } // 创建备忘录 public Memento createMemento() { return new Memento(this.state); } // 恢复备忘录 public void recoveryMemento(Memento memento) { this.setState(memento.getState()); } } 备忘录(Memento) public class Memento { private String state; public Memento(String state) { this.state = state; } public String getState() { return state; } public void setState(String state) { this.state = state; } } 管理者(Caretaker) public class Caretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }客户端
Originator o = new Originator(); o.setState("On"); o.show(); Caretaker c = new Caretaker(); c.setMemento(o.createMemento()); o.setState("Off"); // 数据改变了 o.show(); o.recoveryMemento(c.getMemento()); // 数据恢复 o.show();这就是将保存的细节给封装在了Memento中了,若需要更改保存状态也不用影响客户端了。
Memento 模式适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分。
游戏进度备忘结构图
具体实现
GameRole public class GameRole { private int vitality; // 生命 private int attack; // 攻击力 private int defense; // 防御力 ... // 保存状态 public RoleStateMemento saveState() { return new RoleStateMemento(this.vitality, this.attack, this.defense); } // 恢复 public void recoveryState(RoleStateMemento memento) { this.vitality = memento.getVitality(); this.attack = memento.getAttack(); this.defense = memento.getDefense(); } } RoleStateMemento 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; } } RoleStateCaretaker 管理者 public class RoleStateCaretaker { private RoleStateMemento memento; public RoleStateMemento getMemento() { return memento; } public void setMemento(RoleStateMemento memento) { this.memento = memento; } }客户端
GameRole role = new GameRole(); role.getInitState(); role.displayState(); // 保存记录 RoleStateCaretaker c = new RoleStateCaretaker(); c.setMemento(role.saveState()); // 战斗 role.fight(); role.displayState(); // 游戏进度重新恢复 role.recoveryState(c.getMemento()); role.displayState();Tips:备忘录模式也是有缺点的,角色状态需要完整存储到备忘录对象中,如果状态数据太多,那么在资源消耗上,备忘录模式会非常耗内存,注意你的服务器别崩了
游戏进度恢复--备忘录模式由讯客互联人工智能栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“游戏进度恢复--备忘录模式”