标签:show over 管理器 manager == str mat ossh color
备忘录模式主要用于存档。游戏中我们打boss前总会存档,如果打boss失败,则读取存档,重新挑战boss。
可以看出来,备忘录模式一般包括如下数据结构
1、 存档文件:用于恢复备份场景的必要数据;
2、 存档管理器:用于管理存档,包括存档的读写与展示;
3、 被存档的对象。
下面以射击游戏的存档来举例:
1、 射击选手
package com.coshaho.learn.memorandum; // 射击手 public class Shooter { // 血量 private int blood = 100; // 子弹数量 private int bullet = 100; // 射击 public void shoot() { bullet = bullet - (int) (Math.random()*30); if(0 > bullet) { bullet = 0; } bossHurt(); } // boss伤害 private void bossHurt() { blood = blood - (int) (Math.random()*25); if(0 > blood) { blood = 0; } } public void show() { System.out.println("Shooter blood is " + blood + ", bullet is " + bullet + ‘.‘); } // 保存 public Memorandum save() { return new Memorandum(blood, bullet); } // 载入 public void recovery(Memorandum memory) { if(null == memory) { return; } blood = memory.getBlood(); bullet = memory.getBullet(); } }
2、 存档文件
package com.coshaho.learn.memorandum; // 存档 public class Memorandum { private int blood; private int bullet; public Memorandum(int blood, int bullet) { this.blood = blood; this.bullet = bullet; } public int getBlood() { return blood; } public int getBullet() { return bullet; } }
3、 存档管理器
package com.coshaho.learn.memorandum; // 存档管理器 public class MemorandumManager { private Memorandum memory; public Memorandum getMemory() { return memory; } public void setMemory(Memorandum memory) { this.memory = memory; } }
4、 测试代码
package com.coshaho.learn.memorandum; public class MemorandumTest { public static void main(String[] args) { MemorandumManager mmemoryManager = new MemorandumManager(); Shooter shooter = new Shooter(); shooter.shoot(); shooter.show(); mmemoryManager.setMemory(shooter.save()); shooter.shoot(); shooter.show(); shooter.recovery(mmemoryManager.getMemory()); shooter.show(); } }
标签:show over 管理器 manager == str mat ossh color
原文地址:http://www.cnblogs.com/coshaho/p/6974032.html