一、备忘录模式
1、定义
备忘录模式(Memento Pattern)又称作快照模式(Snapshot Pattern),指在不破坏封装的前提下,捕获一个对象的内部状态,并在对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态,属于行为型设计模式。
状态模式的本质是从发起人实体类(Originator)隔离存储功能,降低实体类的职责。
2、结构
(1)模式的结构
主要角色如下:
- 发起人角色(Originator):负责创建一个备忘录,记录自身需要保存的状态,具备状态回滚功能。
- 备忘录角色(Memento):用于存储 Originator的内部状态,且可以防止 Originator以外的对象进行访问。
- 备忘录管理员角色(Caretaker):负责存储、提供管理 Memento,无法对 Memento的内部进行操作个访问。
3、优缺点
优点:
- 简化发起人实体类(Originator)的职责,隔离状态存储与获取,实现了信心的封装,客户端无须关心状态的保存细节。
- 提供状态的回滚功能。
缺点:
- 消耗资源:如果需要保存的状态过多,则每一次保存都会消耗很多内存。
4、使用场景
- 需要保存历史快照的场景。
- 希望在对象之外保存状态,且除了自己,其他类 对象无法访问状态保存的具体内容。
二、模式的通用实现
代码如下:
public class MementoPattern {public static void main(String[] args) {Originator originator = new Originator();originator.setState("init do something");//保存备忘录IMemento memento = originator.createMemento();Caretaker caretaker = new Caretaker();caretaker.storeMemento(memento);System.out.println("--------第一次保存,状态如下:---------");System.out.println(originator.getState());//回滚originator.restoreMemento(caretaker.getMemento());System.out.println("-------回滚之后---------");}
}//发起人角色
class Originator{//内部状态private String state;public String getState() {return state;}public void setState(String state) {this.state = state;}public IMemento createMemento(){return new Memento(this.state);}public void restoreMemento(IMemento memento){this.state = ((Memento) memento).state;}static class Memento implements IMemento{private String state;public Memento(String state) {this.state = state;}public String getState() {return state;}public void setState(String state) {this.state = state;}}
}// 备忘录角色
interface IMemento{}//备忘录管理员角色
class Caretaker{private IMemento memento;public IMemento getMemento() {return memento;}public void storeMemento(IMemento memento){this.memento = memento;}
}
三、模式的应用实例
以草稿箱为例。
(1)编辑器 - 发起人角色
@Data
@AllArgsConstructor
@ToString
public class Editor {private String title;private String content;public ArticleMemento saveToMemento(){ArticleMemento articleMemento = new ArticleMemento(this.title, this.content);return articleMemento;}public void undoFromMemento(ArticleMemento articleMemento){this.title = articleMemento.getTitle();this.content = articleMemento.getContent();}}
(2)文章-备忘录角色
@AllArgsConstructor
@ToString
@Getter
public class ArticleMemento {private String title;private String content;}
(3)草稿箱 - 备忘录管理员角色
public class DraftsBox {// 存储 - 后进先出private final Stack<ArticleMemento> STACK = new Stack<>();public ArticleMemento getMemento() {ArticleMemento articleMemento = STACK.pop();return articleMemento;}public void addMemento(ArticleMemento articleMemento) {STACK.push(articleMemento);}
}
(4)测试
public static void main(String[] args) {DraftsBox draftsBox = new DraftsBox();Editor editor = new Editor("init title", "首次发表");ArticleMemento articleMemento = editor.saveToMemento();draftsBox.addMemento(articleMemento);System.out.println("--------第一次编辑,保存草稿之后,信息如下:---------");System.out.println(editor);editor.setTitle("one update");editor.setContent("第一次修改");ArticleMemento articleMemento1 = editor.saveToMemento();draftsBox.addMemento(articleMemento1);System.out.println("--------第一次修改,保存草稿之后,信息如下:---------");System.out.println(editor);editor.setTitle("two update");editor.setContent("第二次修改");//ArticleMemento articleMemento2 = editor.saveToMemento();//draftsBox.addMemento(articleMemento2);System.out.println("--------第一次修改,还没保存草稿,信息如下:---------");System.out.println(editor);System.out.println("-------撤销操作---------");ArticleMemento memento = draftsBox.getMemento();editor.undoFromMemento(memento);System.out.println("--------第一次撤销,之后,信息如下:---------");System.out.println(editor);ArticleMemento memento2 = draftsBox.getMemento();editor.undoFromMemento(memento2);System.out.println("--------第二次撤销,之后,信息如下:---------");System.out.println(editor);}

– 求知若饥,虚心若愚。


















