Originator(发起者):负责创建一个Memento,用以记录当前时刻它的内部状态,并可以使用Memento恢复他的内部状态。Originator可以根据需要决定Memento存储Orignator的哪些内部状态。
Memento(备忘录):负责存储Orignator对象的内部状态,并可以防止除了Orignator对象的其他对象访问备忘录(Memento)。备忘录有两个角色,CareTaker只能看到它的窄接口,它只能将备忘录传递给其他对象,Orignator能够看到一个宽接口,允许它访问返回到先前状态的所有数据。
CareTaker(备忘录管理者)负责保存备忘录Memento,不能对备忘录的数据进行操作和检查。
这里以游戏中的保存进度为例子:我们知道打boss之前需要保存进度,以防止失败之后还得重新开始,保存了进度之后,只需要从保存进度的位置,继续去打boss即可。
发起者角色类:
//原始角色 public class Role { int level = 1;// 等级 int blood = 100;// 血量 int attackability = 100;// 攻击力 int defenceability = 100;// 防御力 String Equipment = "木剑";// 装备 String name;// 角色名称 public Role(String name) { this.name = name; } public int getLevel() { return level; } public void setLevel(int level) { this.level = level; } public int getBlood() { return blood; } public void setBlood(int blood) { this.blood = blood; } public int getAttackability() { return attackability; } public void setAttackability(int attackability) { this.attackability = attackability; } public int getDefenceability() { return defenceability; } public void setDefenceability(int defenceability) { this.defenceability = defenceability; } public String getEquipment() { return Equipment; } public void setEquipment(String equipment) { Equipment = equipment; } // 创建备忘录 public RoleMemento CreateMemento() { return new RoleMemento(level, blood, attackability, defenceability, Equipment); } // 恢复保存的进度 public void setMemento(RoleMemento memento) { this.level = memento.level; this.blood = memento.blood; this.attackability = memento.attackability; this.defenceability = memento.defenceability; this.Equipment = memento.Equipment; } public void ShowAbility() { System.out.println(name + "的属性:" + "血量:" + blood + "等级:" + level + "攻击力:" + attackability + "防御力:" + defenceability + "装备:" + Equipment); } //李逍遥打boss失败 public void fightBoss() { this.level = 100; this.blood = 0; this.attackability = 1000; this.defenceability = 10000; this.Equipment = "无"; } }备忘录:
//备忘录角色 public class RoleMemento { int level; int blood; int attackability; int defenceability; String Equipment; public RoleMemento(int level, int blood, int attackability, int defenceability, String Equipment) { this.level = level; this.blood = blood; this.attackability = attackability; this.defenceability = defenceability; this.Equipment = Equipment; } public int getLevel() { return level; } public int getBlood() { return blood; } public int getAttackability() { return attackability; } public int getDefenceability() { return defenceability; } public String getEquipment() { return Equipment; } }备忘录管理者:
public class CareTaker { // 备忘录 管理类 RoleMemento memento; public CareTaker() { } public RoleMemento getMemento() { return memento; } public void setMemento(RoleMemento memento) { this.memento = memento; } }
public static void main(String[] args) { //李逍遥的初始属性 Role r=new Role("李逍遥"); r.ShowAbility(); //李逍遥经过一段时间的努力升级,打怪成长了 r.setLevel(100); r.setBlood(10000); r.setAttackability(10000); r.setDefenceability(10000); r.setEquipment("神器:轩辕剑"); r.ShowAbility();//李逍遥目前的属性 //李逍遥要打boss了,需要保存进度,防止失败了,重新开始 CareTaker c=new CareTaker(); c.memento=r.CreateMemento(); //李逍遥打boss失败了呢 r.fightBoss(); r.ShowAbility();//目前属性:李逍遥的属性:血量:0等级:100攻击力:1000防御力:10000装备:无 血量变0,神器掉落了,后果很严重啊,难道要重新开始 //哎呀,幸好保存了进度,我们读取进度,继续打boss r.setMemento(c.getMemento()); r.ShowAbility();//我李逍遥又满状态复活了,轩辕剑又回来了 李逍遥的属性:血量:10000等级:100攻击力:10000防御力:10000装备:神器:轩辕剑 }