一、模式定义
备忘录模式(Memento Pattern)是一种行为型设计模式,它在不破坏封装性的前提下,捕获并保存一个对象的内部状态,以便在未来将对象恢复到之前保存的状态。因此,备忘录模式也被称为快照模式(Snapshot Pattern)或Token模式。
二、模式角色
备忘录模式包含以下角色:
Originator(原发器):负责创建备忘录对象,并在需要时恢复状态。Memento(备忘录):存储原发器的内部状态。Caretaker(负责人):负责保存备忘录对象。

三、模式分析
备忘录模式主要用于备份和回退操作,常见于软件的回退功能。通过备忘录模式,系统可以回退到某个特定的历史状态,提升用户体验。
备忘录对象用于存储另一个对象的内部状态快照,因此备忘录模式也被称为快照模式或Token模式。
典型代码如下:
原发器类:
public class Originator { private String state; public Originator() {} // 创建一个备忘录对象 public Memento createMemento() { return new Memento(this); } // 根据备忘录对象恢复原发器状态 public void restoreMemento(Memento m) { state = m.state; } public void setState(String state) { this.state = state; } public String getState() { return this.state; }}
备忘录类:
千帆大模型平台
面向企业开发者的一站式大模型开发及服务运行平台
0 查看详情
public class Memento { private String state; public Memento(Originator o) { state = o.state; } public void setState(String state) { this.state = state; } public String getState() { return this.state; }}
负责人类:
import java.util.ArrayList;import java.util.List;public class CareTaker { private List mementoList = new ArrayList(); public void add(Memento state) { mementoList.add(state); } public Memento get(int index) { return mementoList.get(index); }}
四、模式例子
以下是来自《设计模式》一书的例子:
原发器类,创建备忘录类:
package dp.memento;public class UserInfoDTO { private String account; private String password; private String telNo; public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this.telNo = telNo; } public Memento saveMemento() { return new Memento(account, password, telNo); } public void restoreMemento(Memento memento) { this.account = memento.getAccount(); this.password = memento.getPassword(); this.telNo = memento.getTelNo(); } public void show() { System.out.println("Account: " + this.account); System.out.println("Password: " + this.password); System.out.println("TelNo: " + this.telNo); }}
备忘录类,保存原发器类状态:
package dp.memento;class Memento { private String account; private String password; private String telNo; public Memento(String account, String password, String telNo) { this.account = account; this.password = password; this.telNo = telNo; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTelNo() { return telNo; } public void setTelNo(String telNo) { this.telNo = telNo; }}
负责人类,创建备忘录:
package dp.memento;public class Caretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; }}
五、模式应用
备忘录模式在软件中广泛应用,包括:
软件中的存档操作Windows 中的 Ctrl + Z 撤销操作浏览器中的后退功能数据库的事务管理
以上就是设计模式之备忘录模式(行为型)的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/273890.html
微信扫一扫
支付宝扫一扫