参考:http://c.biancheng.net/view/1400.html
什么是备忘录模式
备忘录(Memento)模式的定义:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。该模式又叫快照模式。
优点:
- 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史的状态。
- 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息。
- 简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
缺点:资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。
结构与实现
备忘录模式的主要角色如下。
- 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。
- 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
- 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。

代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class MementoPattern { public static void main(String[] args) { Originator or = new Originator(); Caretaker cr = new Caretaker(); or.setState("S0"); System.out.println("初始状态:" + or.getState()); cr.setMemento(or.createMemento()); or.setState("S1"); System.out.println("新的状态:" + or.getState()); or.restoreMemento(cr.getMemento()); System.out.println("恢复状态:" + or.getState()); } }
class Memento { private String state; public Memento(String state) { this.state = state; } public void setState(String state) { this.state = state; } public String getState() { return state; } }
class Originator { private String state; public void setState(String state) { this.state = state; } public String getState() { return state; } public Memento createMemento() { return new Memento(state); } public void restoreMemento(Memento m) { this.setState(m.getState()); } }
class Caretaker { private Memento memento; public void setMemento(Memento m) { memento = m; } public Memento getMemento() { return memento; } }
|
运行结果:
应用实例
【例1】利用备忘录模式设计相亲游戏。
分析:假如有西施、王昭君、貂蝉、杨玉环四大美女同你相亲,你可以选择其中一位作为你的爱人;当然,如果你对前面的选择不满意,还可以重新选择,但希望你不要太花心;这个游戏提供后悔功能,用“备忘录模式”设计比较合适。

代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| public class DatingGame { public static void main(String[] args) { You you = new You(); GirlStack stack = new GirlStack(); you.setWife("001"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.setWife("002"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.setWife("003"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.setWife("004"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.setWife("005"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.setWife("006"); stack.push(you.createMemento()); System.out.println(you.getWife()); you.restoreMemento(stack.pop()); System.out.println(you.getWife()); you.restoreMemento(stack.pop()); System.out.println(you.getWife()); you.restoreMemento(stack.pop()); System.out.println(you.getWife()); you.restoreMemento(stack.pop()); System.out.println(you.getWife()); you.restoreMemento(stack.pop()); System.out.println(you.getWife()); } }
class Girl { private String name; public Girl(String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } }
class You { private String wifeName; public void setWife(String name) { wifeName = name; } public String getWife() { return wifeName; } public Girl createMemento() { return new Girl(wifeName); } public void restoreMemento(Girl p) { setWife(p.getName()); } }
class GirlStack { private Girl girls[]; private int top; public GirlStack() { girls = new Girl[5]; top = -1; } public boolean push(Girl p) { if (top >= 4) { System.out.println("你太花心了,变来变去的!"); return false; } else { girls[++top] = p; return true; } } public Girl pop() { if (top <= 0) { System.out.println("数组空了!"); return girls[0]; } else{ return girls[top--]; } } }
|
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13
| 001 002 003 004 005 你太花心了,变来变去的! 006 005 004 003 002 数组空了! 001
|
应用场景
- 需要保存与恢复数据的场景,如玩游戏时的中间结果的存档功能。
- 需要提供一个可回滚操作的场景,如 Word、记事本、Photoshop,Eclipse 等软件在编辑时按 Ctrl+Z 组合键,还有数据库中事务操作。
扩展
在前面介绍的备忘录模式中,有单状态备份的例子,也有多状态备份的例子。下面介绍备忘录模式如何同原型模式混合使用。在备忘录模式中,通过定义“备忘录”来备份“发起人”的信息,而原型模式的 clone() 方法具有自备份功能,所以,如果让发起人实现 Cloneable 接口就有备份自己的功能,这时可以删除备忘录类

代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class PrototypeMemento { public static void main(String[] args) { OriginatorPrototype or = new OriginatorPrototype(); PrototypeCaretaker cr = new PrototypeCaretaker(); or.setState("S0"); System.out.println("初始状态:" + or.getState()); cr.setMemento(or.createMemento()); or.setState("S1"); System.out.println("新的状态:" + or.getState()); or.restoreMemento(cr.getMemento()); System.out.println("恢复状态:" + or.getState()); } }
class OriginatorPrototype implements Cloneable { private String state; public void setState(String state) { this.state = state; } public String getState() { return state; } public OriginatorPrototype createMemento() { return (OriginatorPrototype)this.clone(); } public void restoreMemento(OriginatorPrototype opt) { this.setState(opt.getState()); }
@Override public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return null; } }
class PrototypeCaretaker { private OriginatorPrototype opt; public void setMemento(OriginatorPrototype opt) { this.opt = opt; } public OriginatorPrototype getMemento() { return opt; } }
|
运行结果: