备忘录(Memento):在不破坏封装性的前提性,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。
Originator(发起人):创建一个备忘录 Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator 可根据需要决定 Memento存储 Originator 的哪些内部状态。
Memento(备忘录):负责存储 Originator 对象的内部状态,并防止 Originator 以外的其他对象访问备忘录 Memento。
Caretaker(管理者):负责保存好备忘录 Memento。
基本代码
发起人类
class Originator {//状态private String state;public String getState(){return this.state;}public void setState(String value){this.state = value;}//显示数据public void show(){System.out.println("State:"+this.state);}//创建备忘录public Memento createMemento(){return new Memento(this.state);}//恢复备忘录public void recoveryMemento(Memento memento){this.setState(memento.getState());}
}
备忘录类
class Memento {private String state;public Memento (String state){this.state = state;}public String getState(){return this.state;}public void setState(String value){this.state = value;}
}
管理者
class Caretaker{private Memento memento;public Memento getMemento(){return this.memento;}public void setMemento(Memento value){this.memento = value;}
}
客户端
public class Test { public static void main(String[] args) {System.out.println("**********************************************"); System.out.println("《大话设计模式》代码样例");System.out.println(); // Originator初始状态,状态属性为"On"Originator o = new Originator();o.setState("On");o.show();Caretaker c = new Caretaker();// 保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节c.setMemento(o.createMemento());// Originator改变了状态属性为"Off"o.setState("Off");o.show();// 恢复原初始状态o.recoveryMemento(c.getMemento());o.show();System.out.println();System.out.println("**********************************************");}
}
将要保存的细节给封装到 Memento 中,如果想要修改要保存的细节,只需要更改 Memento 类,不用更改客户端。
Memento 模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator 可以根据保存的Memento信息还原到前一状态。
应用场景
在系统中使用命令模式,需要实现命令的撤销功能,命令模式可以使用备忘录模式来存储可撤销操作的状态。
GameRole 游戏角色类。
RoleStateMemento 角色备忘录类。
RoleStateCaretaker 角色状态管理者类。
游戏角色类
class GameRole {// 生命力private int vitality;public int getVitality(){return this.vitality;}public void setVitality(int value){this.vitality = value;}// 攻击力private int attack;public int getAttack(){return this.attack;}public void setAttack(int value){this.attack = value;}// 防御力private int defense;public int getDefense(){return this.defense;}public void setDefense(int value){this.defense = value;}// 状态显示public void displayState(){System.out.println("角色当前状态:");System.out.println("体力:"+this.vitality);System.out.println("攻击力:"+this.attack);System.out.println("防御力:"+this.defense);System.out.println();}// 获得初始状态(数据通常来自本机磁盘或远程数据接口)public void getInitState(){this.vitality = 100;this.attack = 100;this.defense = 100;}// 战斗(在与Boss大战后游戏数据损耗为0)public void fight(){this.vitality = 0;this.attack = 0;this.defense = 0;}// 保存角色状态public RoleStateMemento saveState(){// 保存状态return new RoleStateMemento(this.vitality,this.attack,this.defense);}// 恢复角色状态public void recoveryState(RoleStateMemento memento){this.setVitality(memento.getVitality());this.setAttack(memento.getAttack());this.setDefense(memento.getDefense());}
}
角色状态存储箱
class RoleStateMemento {// 存储了要保存的状态private int vitality;private int attack;private int defense;// 将生命力、攻击力、防御力存入状态存储箱对象中public RoleStateMemento (int vitality,int attack,int defense){this.vitality = vitality;this.attack = attack;this.defense = defense;}// 生命力public int getVitality(){return this.vitality;}public void setVitality(int value){this.vitality = value;}// 攻击力public int getAttack(){return this.attack;}public void setAttack(int value){this.attack = value;}// 防御力public int getDefense(){return this.defense;}public void setDefense(int value){this.defense = value;}
}
角色状态管理者
class RoleStateCaretaker{private RoleStateMemento memento;public RoleStateMemento getRoleStateMemento(){return this.memento;}public void setRoleStateMemento(RoleStateMemento value){this.memento = value;}
}
客户端
public class Test {public static void main(String[] args){System.out.println("**********************************************"); System.out.println("《大话设计模式》代码样例");System.out.println(); // 大战Boss前GameRole role = new GameRole();role.getInitState();role.displayState();// 保存进度RoleStateCaretaker stateAdmin = new RoleStateCaretaker();stateAdmin.setRoleStateMemento(role.saveState());// 大战Boss时,损耗严重role.fight();// 显示状态role.displayState();// 游戏进度恢复role.recoveryState(stateAdmin.getRoleStateMemento());// 显示状态role.displayState();System.out.println();System.out.println("**********************************************");}
}