备忘录模式又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。
备忘录有两个等效的接口:
窄接口:管理者对象(和其他发起人对象之外的任何对象)看到的是备忘录的窄接口,这个窄接口只允许他把备忘录对象传给其他的对象。
宽接口:与管理者看到的窄接口相反,发起人对象可以看到一个宽接口,这个宽接口允许它读取所有的数据,以便根据这些数据恢复这个发起人对象的内部状态。
发起人
public class Role { private int vitality; private int sword; public void initState() { vitality = 100; sword = 100; } public void fight() { vitality = 0; sword = 0; } public StateMemento saveState() { return new StateMemento(vitality, sword); } public void recoveState(StateMemento memento) { this.vitality = memento.getVitality(); this.sword = memento.getSword(); } public void showState() { System.out.println("生命力:" + vitality + ",战斗力:" + sword); }}
备忘录
public class StateMemento { private int vitality; private int sword; public StateMemento(int vitality, int sword) { this.vitality = vitality; this.sword = sword; } public int getVitality() { return vitality; } public void setVitality(int vitality) { this.vitality = vitality; } public int getSword() { return sword; } public void setSword(int sword) { this.sword = sword; }}
管理者
public class MementoManager { private StateMemento memento; public StateMemento getMemento() { return memento; } public void setMemento(StateMemento memento) { this.memento = memento; }}
测试类
public class Test { public static void main(String[] args) { System.out.println("-----战斗前-----"); Role role = new Role(); role.initState(); role.showState(); MementoManager manager = new MementoManager(); manager.setMemento(role.saveState()); System.out.println("-----战斗后-----"); role.fight(); role.showState(); System.out.println("-----恢复后-----"); role.recoveState(manager.getMemento()); role.showState(); }}
注:“白箱”备忘录模式是破坏封装性的。但是通过程序员自律,同样可以在一定程度上实现模式的大部分用意。2、“黑箱”备忘录模式类图“黑箱”备忘录模式代码如下窄接口
public interface Memento {}
发起人
public class Role { private int vitality; private int sword; public void initState() { vitality = 100; sword = 100; } public void fight() { vitality = 0; sword = 0; } public Memento saveState() { return new StateMemento(vitality, sword); } public void recoveState(Memento memento) { StateMemento stateMemento = (StateMemento) memento; this.vitality = stateMemento.getVitality(); this.sword = stateMemento.getSword(); } public void showState() { System.out.println("生命力:" + vitality + ",战斗力:" + sword); } private class StateMemento implements Memento { private int vitality; private int sword; public StateMemento(int vitality, int sword) { this.vitality = vitality; this.sword = sword; } public int getVitality() { return vitality; } public void setVitality(int vitality) { this.vitality = vitality; } public int getSword() { return sword; } public void setSword(int sword) { this.sword = sword; } }}
管理者
public class MementoManager { private Memento mem(话后面加jpg是什么梗?jpg是图片格式,jpg后缀一般在形容词后面,文字+jpg的描述形式是用文字给出一种画面感,更显生动。)ento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; }}
测试类
public class Test { public static void main(String[] args) { System.out.println("----