(1)概述
备忘录模式又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便需要时能将该对象恢复到之前保存的状态
(2)结构
- 发起人角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录的功能,它可以访问备忘录里的所有信息
- 备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人
- 管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录没有读写权限
备忘录有两个等效接口:
- 窄接口:管理者对象(和其他除发起人对象之外的任何对象),看到的是窄接口,该接口只允许把备忘录对象传递给其他对象
- 宽接口:发起人对象可以看到宽接口,该接口允许读取所有的数据,以便恢复对象之前的内部状态
(3)案例
以游戏存档为例:
“白箱”备忘录
备忘录角色对任何对象都提供宽接口(破坏了封装性)
发起人角色
public class GameRole {
/**
* 生命值
*/
private int vit;
/**
* 攻击值
*/
private int atk;
/**
* 防御值
*/
private int def;
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;
}
/**
* 初始化内部状态
*/
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
/**
* 战斗
*/
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
/**
* 保存游戏角色状态
*
* @return
*/
public RoleStateMemento saveState() {
return new RoleStateMemento(vit, atk, def);
}
/**
* 恢复角色状态
*
* @param roleStateMemento
*/
public void recoverState(RoleStateMemento roleStateMemento) {
this.vit = roleStateMemento.getVit();
this.atk = roleStateMemento.getAtk();
this.def = roleStateMemento.getDef();
}
/**
* 展示角色状态
*/
public void displayState() {
System.out.println("生命值:" + vit);
System.out.println("攻击值:" + vit);
System.out.println("防御值:" + vit);
}
}
备忘录角色
public class RoleStateMemento {
/**
* 生命值
*/
private int vit;
/**
* 攻击值
*/
private int atk;
/**
* 防御值
*/
private int def;
public RoleStateMemento() {
}
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
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;
}
}
备忘录管理者
public class RoleStateCaretaker {
private RoleStateMemento roleStateMemento;
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
Client
public class Client {
public static void main(String[] args) {
System.out.println("====战斗前====");
GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.displayState();
//备份角色状态
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setRoleStateMemento(gameRole.saveState());
System.out.println("====战斗后====");
gameRole.fight();
gameRole.displayState();
System.out.println("====恢复状态====");
gameRole.recoverState(roleStateCaretaker.getRoleStateMemento());
gameRole.displayState();
}
}
- “黑箱”备忘录
备忘录角色对发起人提供宽接口,对其他对象提供窄接口(即将备忘录类设置为发起人的内部类)
发起人角色
public class GameRole {
/**
* 生命值
*/
private int vit;
/**
* 攻击值
*/
private int atk;
/**
* 防御值
*/
private int def;
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;
}
/**
* 初始化内部状态
*/
public void initState() {
this.vit = 100;
this.atk = 100;
this.def = 100;
}
/**
* 战斗
*/
public void fight() {
this.vit = 0;
this.atk = 0;
this.def = 0;
}
/**
* 保存游戏角色状态
*
* @return
*/
public Memento saveState() {
return new RoleStateMemento(vit, atk, def);
}
/**
* 恢复角色状态
*
* @param memento
*/
public void recoverState(Memento memento) {
RoleStateMemento roleStateMemento = (RoleStateMemento) memento;
this.vit = roleStateMemento.getVit();
this.atk = roleStateMemento.getAtk();
this.def = roleStateMemento.getDef();
}
/**
* 展示角色状态
*/
public void displayState() {
System.out.println("生命值:" + vit);
System.out.println("攻击值:" + vit);
System.out.println("防御值:" + vit);
}
private class RoleStateMemento implements Memento {
/**
* 生命值
*/
private int vit;
/**
* 攻击值
*/
private int atk;
/**
* 防御值
*/
private int def;
public RoleStateMemento() {
}
public RoleStateMemento(int vit, int atk, int def) {
this.vit = vit;
this.atk = atk;
this.def = def;
}
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;
}
}
}
备忘录接口(对外提供的窄接口)
public interface Memento {
}
备忘录管理者角色
public class RoleStateCaretaker {
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
Client
public class Client {
public static void main(String[] args) {
System.out.println("====战斗前====");
GameRole gameRole = new GameRole();
gameRole.initState();
gameRole.displayState();
//备份角色状态
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setMemento(gameRole.saveState());
System.out.println("====战斗后====");
gameRole.fight();
gameRole.displayState();
System.out.println("====恢复状态====");
gameRole.recoverState(roleStateCaretaker.getMemento());
gameRole.displayState();
}
}
(4)优缺点
- 提供了一种可恢复状态的机制,当用户需要事,可方便的恢复到某个历史状态
- 实现了内部状态的封装,除发起人之外的角色对这些状态信息都没有读写权限
- 简化了发起人类,内部状态由管理者进行管理,发起人无需关心,复合单一职责原则
- 当需要存储的状态信息及频率过多过大时,需要消耗更多的资源
(5)使用场景
- 需要保存与恢复数据时
- 需要提供可回滚操作时,如
ctrl + z
、数据库回滚等
评论 (0)