设计模式学习笔记——备忘录模式
2016-03-23 23:40
381 查看
备忘录模式
备忘录模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。结构图
代码实现
备忘录类Memento:/** * 备忘录:负责储存原始对象的内部状态 * @author xukai * 2016年3月23日 下午6:44:26 */ 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; } }需要保存状态的对象:
/** * 保存对象:负责创建备忘录 * @author xukai * 2016年3月23日 下午6:44:07 */ public class Originator { private String state; public String getState() { return state; } public void setState(String state) { this.state = state; } public Memento createMemento(){ return new Memento(state); } public void setMemento(Memento memento){ state = memento.getState(); } public void show(){ System.out.println("state=" + state); } }管理者:执行保存备忘录操作
/** * 管理者:保存备忘录 * @author xukai * 2016年3月23日 下午10:55:32 */ public class Caretaker { private Memento memento; /** * @return */ public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }客户端:
public class Client { public static void main(String[] args) { Originator o = new Originator(); o.setState("on"); o.show(); Caretaker c = new Caretaker(); c.setMemento(o.createMemento()); o.setState("off"); o.show(); o.setMemento(c.getMemento()); o.show(); } }Console:
state=on state=off state=on首先,创建一个初识对象,赋给其某种状态。
其次,使用管理者,执行保存备忘录操作,记录当前原始对象的状态。
接着,原始对象的状态由于某种原因发生改变,但是需要将状态恢复到保存备忘录的状态。
最后,原始对象通过管理者中的备忘录,获取保存的状态。
demo
问题:使用小霸王游戏机在打大BOSS之前一般都有存档,Game Over之后,可以回到存档位置,重新打BOSS。结构图
代码实现
角色状态存储:/** * 角色备忘录:角色状态存储 * @author xukai * 2016年3月23日 下午11:17:56 */ public class RoleStateMemento { private int vit; // 生命力 private int atk; // 攻击力 private int def; // 防御力 public RoleStateMemento(int vit, int atk, int def) { this.vit = vit; this.atk = atk; this.def = def; } public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } }游戏角色:
/** * 游戏角色 * @author xukai * 2016年3月23日 下午11:19:37 */ public class GameCharacters { private int vit; // 生命力 private int atk; // 攻击力 private int def; // 防御力 // 初始化 public void init(){ this.vit = 100; this.atk = 100; this.def = 100; } // 大战之后 public void fight(){ this.vit = 0; this.atk = 0; this.def = 0; } public RoleStateMemento saveState(){ return new RoleStateMemento(vit, atk ,def); } public void recoveryState(RoleStateMemento roleStateMemento){ this.vit = roleStateMemento.getVit(); this.atk = roleStateMemento.getAtk(); this.def = roleStateMemento.getDef(); } @Override public String toString() { return "GameCharacters [vit=" + vit + ", atk=" + atk + ", def=" + def + "]"; } }管理员:
/** * 管理员 * * @author xukai 2016年3月23日 下午11:23:14 */ public class RoleStateCaretaker { private RoleStateMemento memento; public RoleStateMemento getMemento() { return memento; } public void setMemento(RoleStateMemento memento) { this.memento = memento; } }客户端测试:
public class Client { public static void main(String[] args) { // 准备打BOSS GameCharacters characters = new GameCharacters(); characters.init(); System.out.println(characters); // 保存进度 RoleStateCaretaker caretaker = new RoleStateCaretaker(); caretaker.setMemento(characters.saveState()); // 打BOSS失败 characters.fight(); System.out.println(characters); //重新开始,读存档 characters.recoveryState(caretaker.getMemento()); System.out.println(characters); } }Console:
GameCharacters [vit=100, atk=100, def=100]
GameCharacters [vit=0, atk=0, def=0]
GameCharacters [vit=100, atk=100, def=100]
总结
Memento模式适用于比较复杂的、但需要维护或记录属性历史的类。或者需要保存的属性只是一小部分,Originator可以通过这些小部分信息还原到上一状态。另外,适用备忘录可以将复杂对象的内部信息对其他对象屏蔽。
当角色的状态改变的时候,有可能这个状态无效,那么就可以使用备忘录暂时存储起来,方便状态复原。
相关文章推荐
- code vs 1343 蚱蜢
- Nodejs学习笔记(一)
- 剑指offer——二进制中1的个数
- 第一次实验
- [leetcode 48] Rotate Image--------矩阵旋转90度
- 学习java的第一天
- Broadcast机制简介
- C语言知识点回顾
- **stack smashing detecting**
- java基础学习03(java基础程序设计)
- 练习1——贪心算法总结
- 《世界是数字的》读后感
- 为Github账户设置SSH key
- redis pool config的配置参数
- 程序员的自我修养——Windows下的动态链接
- [List]——两种遍历方式
- 优化myeclipse启动速度以及解决内存不足问题
- 作业三
- 分布式入门之1:Lease机制
- Shell glob