# Memento(备忘录模式) # ## 概述 ## 备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。 ## 使用 ## ### 实现 ### 1. 创建一个原始类 ```Java public class Original { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Original(String value) { this.value = value; } public Memento createMemento(){ return new Memento(value); } public void restoreMemento(Memento memento){ this.value = memento.getValue(); } } ``` 2. 创建备忘录类和存储类 ```Java public class Memento { private String value; public Memento(String value) { this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } ``` ```Java public class Storage { private Memento memento; public Storage(Memento memento) { this.memento = memento; } public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } } ``` 3. 测试 ```Java public class MementoTest { @Test public void testMemento() { // 创建原始类 Original origi = new Original("egg"); // 创建备忘录 Storage storage = new Storage(origi.createMemento()); // 修改原始类的状态 System.out.println("初始化状态为:" + origi.getValue()); origi.setValue("niu"); System.out.println("修改后的状态为:" + origi.getValue()); // 回复原始类的状态 origi.restoreMemento(storage.getMemento()); System.out.println("恢复后的状态为:" + origi.getValue()); } } ``` ## 使用场景 ## 1. 后悔药。 2. 打游戏时的存档。 3. Windows 里的 ctri + z。 4. IE 中的后退。 5. 数据库的事务管理。 ## 主要解决 ## 所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。 ## 优点 ## 1. 给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。 2. 实现了信息的封装,使得用户不需要关心状态的保存细节。 ## 缺点 ## 消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。 ## 注意事项 ## 1. 为了符合迪米特原则,还要增加一个管理备忘录的类。 2. 为了节约内存,可使用原型模式+备忘录模式。