备忘录模式(Memento Pattern)保存一个对象的某个状态,以便在适当的时候恢复对象。备忘录模式属于行为型模式。java
所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象以外保存这个状态,这样能够在之后将这个对象恢复到原先保存的状态。git
备忘录模式主要包含如下几个角色:github
Originator(发起人):负责建立一个备忘录Memento,用以记录当前时刻它的内部状态,并可以使用备忘录恢复状态。Originator可根据需求决定Memento存储Originator的哪些内部状态。this
Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator之外的其余对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其余对象。Originator可以看到一个宽接口,容许它访问返回到先前状态所需的全部数据。code
Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操做或检查。对象
备忘录基本代码接口
发起人(Originator)类游戏
public class Originator { //须要保存的属性 private String state; //建立备忘录,将当前须要保存的信息导入并实例化出一个Memento对象 public Memento createMemento() { return new Memento(state); } //恢复备忘录,将Memento导入并将相关数据恢复 public void setMemento(Memento memento) { this.state = memento.getState(); } //显示数据 public void show() { System.out.println("state = " + state); } public String getState() { return state; } public void setState(String state) { this.state = state; } }
备忘录(Memento)类内存
public class Memento { private String state; //构造方法,将相关数据导入 public Memento(String state) { this.state = state; } public String getState() { return state; } public void setState(String state) { this.state = state; } }
管理者(Caretaker)类资源
public class Caretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }
客户端程序
public class Client { public static void main(String[] args) { Originator originator = new Originator(); originator.setState("On"); originator.show(); Caretaker caretaker = new Caretaker(); caretaker.setMemento(originator.createMemento()); originator.setState("Off"); originator.show(); originator.setMemento(caretaker.getMemento()); originator.show(); } }
运行结果
state = On state = Off state = On
实现场景:游戏中的某个场景,一游戏角色有生命力、攻击力、防护力等数据,在打Boss前和后必定会不同的,咱们容许玩家若是感受与Boss决斗的效果不理想可让游戏恢复到决斗以前。
代码结构图
游戏角色类
public class GameRole { private int vit; //生命力 private int atk; //攻击力 private int 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; } //保存角色状态 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 stateDisplay() { System.out.println("角色生命力:" + vit); System.out.println("角色攻击力:" + atk); System.out.println("角色防护力:" + 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 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 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; } }
客户端程序
public class Client { public static void main(String[] args) { System.out.println("------------大战Boss前------------"); //大战Boss前 GameRole gameRole = new GameRole(); gameRole.initState(); gameRole.stateDisplay(); //保存进度 RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker(); roleStateCaretaker.setRoleStateMemento(gameRole.saveState()); System.out.println("------------大战Boss后------------"); //大战Boss时,损耗严重 gameRole.fight(); gameRole.stateDisplay(); System.out.println("------------恢复以前状态------------"); //恢复以前状态 gameRole.recoverState(roleStateCaretaker.getRoleStateMemento()); gameRole.stateDisplay(); } }
运行结果
------------大战Boss前------------ 角色生命力:100 角色攻击力:100 角色防护力:100 ------------大战Boss后------------ 角色生命力:0 角色攻击力:0 角色防护力:0 ------------恢复以前状态------------ 角色生命力:100 角色攻击力:100 角色防护力:100
优势
实现了信息的封装,使得用户不须要关心状态的保存细节。
给用户提供了一种能够恢复状态的机制,可使用户可以比较方便地回到某个历史的状态。
缺点
不少时候咱们老是须要记录一个对象的内部状态,这样作的目的是为了容许用户取消不肯定或者错误的操做,可以恢复到他原先的状态,使得他有"后悔药"可吃。
文章做者:leisurexi
新博客地址:https://leisurexi.github.io/
许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文连接及做者。