文章目录
前言
- 掌握备忘录模式的应用场景
- 掌握备忘录模式在落地实战中的压栈管理
一、定义
备忘录模式(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态
二、应用场景
- 需要保存与恢复数据的场景
- 需要提供一个可回滚操作的场景
三、基本结构
- 发起人角色(Originator):记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息
- 备忘录角色(Memento):负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人
- 管理者角色(Caretaker):对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问和修改
四、基本使用
1. 发起人角色
public class Originator {
private String state;
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public Memento createMemento() {
return new Memento(this.state);
}
public void restoreMemento(Memento memento) {
this.state = memento.getState();
}
}
2. 备忘录角色
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
3. 管理者角色
public class Caretaker {
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
4. 客户端角色
public class Client {
public static void main(String[] args) {
System.out.println("==========初始化=========");
Originator originator = new Originator();
originator.setState("状态1");
System.out.println("当前状态:" + originator.getState());
System.out.println("===========备份==========");
Caretaker caretaker = new Caretaker();
caretaker.setMemento(originator.createMemento());
System.out.println("=========修改状态=========");
originator.setState("状态2");
System.out.println("当前状态:" + originator.getState());
originator.restoreMemento(caretaker.getMemento());
System.out.println("===========回退==========");
System.out.println("当前状态:" + originator.getState());
}
}
五、编辑器保存到草稿
1. 发起人角色(编辑器)
public class Editor {
private String title;
private String content;
public Editor(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public ArticleMemento saveToMemento() {
return new ArticleMemento(this.title, this.content);
}
}
2. 备忘录角色
public class ArticleMemento {
private String title;
private String content;
public ArticleMemento(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
3. 管理者角色(草稿箱)
public class DraftsBox {
private final Stack<ArticleMemento> STACK = new Stack<>();
public ArticleMemento getMemento() {
return this.STACK.pop();
}
public void addMemento(ArticleMemento memento) {
this.STACK.push(memento);
}
}
4. 客户端角色
public class Client {
public static void main(String[] args) {
DraftsBox draftsBox = new DraftsBox();
System.out.println("==========新建文章内容==========");
Editor editor = new Editor("备忘录模式", "在不破坏封装性的前提下,捕获一个对象的内部状态.");
System.out.println(editor.getContent());
System.out.println("==========保存到草稿箱==========");
draftsBox.addMemento(editor.saveToMemento());
System.out.println("==========首次修改文章内容==========");
editor.setContent("在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态");
System.out.println(editor.getContent());
System.out.println("==========保存到草稿箱==========");
draftsBox.addMemento(editor.saveToMemento());
System.out.println("==========二次修改文章内容==========");
editor.setContent("在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态");
System.out.println(editor.getContent());
System.out.println("==========保存到草稿箱==========");
draftsBox.addMemento(editor.saveToMemento());
System.out.println("==========第一次撤销==========");
editor.setContent(draftsBox.getMemento().getContent());
System.out.println(editor.getContent());
System.out.println("==========第二次撤销==========");
editor.setContent(draftsBox.getMemento().getContent());
System.out.println(editor.getContent());
System.out.println("==========第三次撤销==========");
editor.setContent(draftsBox.getMemento().getContent());
System.out.println(editor.getContent());
}
}
总结
1. 优点
- 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史的状态
- 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息
- 简化了发起人类。发起人不需要人管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,符合单一职责原则
2. 缺点
- 资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源