一、备忘录模式
备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。
又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存整个状态,以便以后当需要时能将该对象恢复到原先的保存的状态
二、介绍
意图:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
主要解决:所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。
何时使用:很多时候我们总是需要记录一个对象的内部状态,这样做的目的就是为了允许用户取消不确定或者错误的操作,能够恢复到他原先的状态,使得他有"后悔药"可吃。
如何解决:通过一个备忘录类专门存储对象状态。
关键代码:客户不与备忘录类耦合,与备忘录管理类耦合。
应用实例: 1、后悔药。 2、打游戏时的存档。 3、Windows 里的 ctrl + z。 4、IE 中的后退。 5、数据库的事务管理。
优点: 1、给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。 2、实现了信息的封装,使得用户不需要关心状态的保存细节。
缺点:消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。
使用场景: 1、需要保存/恢复数据的相关状态场景。 2、提供一个可回滚的操作。
注意事项: 1、为了符合迪米特原则,还要增加一个管理备忘录的类。 2、为了节约内存,可使用原型模式+备忘录模式。
三、结构
备忘录模式的主要角色:
1、发起人角色:记录当前时刻的内部状态信息,提供创建备忘录和回复记录功能,实现其他业务功能,它可以访问备忘录里的所有信息
2、备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人
3、管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但不能对备忘录的 内容进行修改和访问。
备忘录有两个等效的接口:
1、窄接口:管理者对象(和其它发起人对象之外的任何对象)看到的时备忘录的窄接口,整个窄接口只允许他把备忘录对象转给其他对象
2、宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽街口,整个宽接口只允许它读取所有的数据,以便根据这些数据恢复整个发起人对象内部的状态
四、白箱备忘录
创建游戏角色
package MP_01_Memento;
/**
* @Author: {LZG}
* @ClassName: GameRole
* @Description: 游戏角色类
* @Date: 2022/4/6 17:36
**/
public class GameRole {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
// 初始化内部状态
public void initState(){
this.atk=100;
this.vit=100;
this.def=100;
}
// 战斗
public void fight(){
this.atk=0;
this.vit=0;
this.def=0;
}
// 保存角色状态功能
public RoleStateMemento saveState(){
return new RoleStateMemento(vit,atk,def);
}
// 恢复角色状态
public void recoverState(RoleStateMemento roleStateMemento){
// 将备忘录中存储的状态赋值给当前对象的成员
this.vit=roleStateMemento.getVit();
this.atk=roleStateMemento.getAtk();
this.def=roleStateMemento.getDef();
}
// 展示状态功能
public void display(){
System.out.println("角色的生命力是"+vit);
System.out.println("角色的攻击力是"+atk);
System.out.println("角色的防御力是"+def);
}
public void setVit(int vit) {
this.vit = vit;
}
public void setAtk(int atk) {
this.atk = atk;
}
public void setDef(int def) {
this.def = def;
}
public int getVit() {
return vit;
}
public int getAtk() {
return atk;
}
public int getDef() {
return def;
}
}
创建备忘录角色
package MP_01_Memento;
/**
* @Author: {LZG}
* @ClassName: RoleStateMemento
* @Description: 备忘录角色类
* @Date: 2022/4/6 17:39
**/
public class RoleStateMemento {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
public RoleStateMemento() {
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
创建备忘录管理者对象
package MP_01_Memento;
/**
* @Author: {LZG}
* @ClassName: RoleStateCaretaker
* @Description: 备忘录对象管理对象
* @Date: 2022/4/6 18:00
**/
public class RoleStateCaretaker {
// 声明RoleStateMemento类型的变量
private RoleStateMemento roleStateMemento;
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
测试
package MP_01_Memento;
/**
* @Author: {LZG}
* @ClassName: Client
* @Description: TODO
* @Date: 2022/4/6 18:01
**/
public class Client {
public static void main(String[] args) {
System.out.println("-----------大战之前----------------");
// 创建游戏角色对象
GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.display();
// 将该游戏角色内部状态进行备份
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setRoleStateMemento(gameRole.saveState());
System.out.println("-----------大战Boos之后----------------");
// 损耗严重
gameRole.fight();
gameRole.display();
System.out.println("-----------恢复之前的状态----------------");
gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
}
}
测试结果
五、黑箱备忘录
定义窄接口
package MP_02MementoBlack;
/**
* @Author: {LZG}
* @ClassName: Memento
* @Description: 备忘录接口,对外提供的窄接口
* @Date: 2022/4/6 18:14
**/
public interface Memento {
}
定义角色类并实现私有的内部类实现接口
package MP_02MementoBlack;
/**
* @Author: {LZG}
* @ClassName: GameRole
* @Description: 游戏角色类
* @Date: 2022/4/6 17:36
**/
public class GameRole {
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
// 初始化内部状态
public void initState(){
this.atk=100;
this.vit=100;
this.def=100;
}
// 战斗
public void fight(){
this.atk=0;
this.vit=0;
this.def=0;
}
// 保存角色状态功能
public Memento saveState(){
return new RoleStateMemento(vit,atk,def);
}
// 恢复角色状态
public void recoverState(Memento memento){
RoleStateMemento roleStateMemento=(RoleStateMemento ) memento;
// 将备忘录中存储的状态赋值给当前对象的成员
this.vit=roleStateMemento.getVit();
this.atk=roleStateMemento.getAtk();
this.def=roleStateMemento.getDef();
}
// 展示状态功能
public void display(){
System.out.println("角色的生命力是"+vit);
System.out.println("角色的攻击力是"+atk);
System.out.println("角色的防御力是"+def);
}
public void setVit(int vit) {
this.vit = vit;
}
public void setAtk(int atk) {
this.atk = atk;
}
public void setDef(int def) {
this.def = def;
}
public int getVit() {
return vit;
}
public int getAtk() {
return atk;
}
public int getDef() {
return def;
}
private class RoleStateMemento implements Memento{
private int vit; // 生命力
private int atk; // 攻击力
private int def; // 防御力
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
public RoleStateMemento() {
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
}
创建备忘录角色管理对象
package MP_02MementoBlack;
/**
* @Author: {LZG}
* @ClassName: RoleStateCaretaker
* @Description: 备忘录对象管理对象
* @Date: 2022/4/6 18:00
**/
public class RoleStateCaretaker {
// 声明RoleStateMemento类型的变量
private Memento Memento;
public Memento getMemento() {
return Memento;
}
public void setMemento(Memento memento) {
Memento = memento;
}
}
测试
package MP_02MementoBlack;
/**
* @Author: {LZG}
* @ClassName: Client
* @Description: TODO
* @Date: 2022/4/6 18:01
**/
public class Client {
public static void main(String[] args) {
System.out.println("-----------大战之前----------------");
// 创建游戏角色对象
GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.display();
// 将该游戏角色内部状态进行备份
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setMemento(gameRole.saveState());
System.out.println("-----------大战Boos之后----------------");
// 损耗严重
gameRole.fight();
gameRole.display();
System.out.println("-----------恢复之前的状态----------------");
gameRole.recoverState(roleStateCaretaker.getMemento());
}
}
测试结果
类图
六、优缺点
优点:
1、提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史状态。
2、实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能访问这些状态信息。
3、简化了发起人类。发起人不需要直接管理和保存其内部状态的各个部分,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
缺点:
资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的资源