一、备忘录模式
1、UML图
2、基本代码
class Originator{
private String state;//需要保存的属性,可能有多个
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento CreateMemento() {
//创建备忘录,将当前需要保存的信息导入并实例化一个Memento对象
return(new Memento(state));
}
public void SetMemento(Memento memento) {
//恢复备忘录,将memento导入相关数据恢复
state=memento.getState();
}
public void Show() {
System.out.println("State="+state);
}
}
class Memento{
private String state;
public Memento(String state) {
super();
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class Caretaker{
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class Main{
public static void main(String[] args){
Originator o=new Originator();
o.setState("On");
o.Show();
Caretaker c=new Caretaker();
c.setMemento(o.CreateMemento());
o.setState("Off");
o.Show();
o.SetMemento(c.getMemento());
o.Show();
}
}
3、优点:
- 有时一些发起人对象的内部信息必须保存在发起人对象以外的地方,但是必须要由发起人对象自己读取,这时,使用备忘录模式可以把复杂的发起人内部信息对其他的对象屏蔽起来,从而可以恰当地保持封装的边界。
- 本模式简化了发起人类。发起人不再需要管理和保存其内部状态的一个个版本,客户端可以自行管理他们所需要的这些状态的版本。
- 当发起人角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。
4、缺点: - 如果发起人角色的状态需要完整地存储到备忘录对象中,那么在资源消耗上面备忘录对象会很大。
- 当管理者角色将一个备忘录存储起来的时候,管理者可能并不知道这个状态会占用多大的存储空间,从而无法提醒用户一个操作是否很大。
5、适用场合 - 如果必须保存一个对象在某一个时刻的全部或者部分状态,方便在以后需要的时侯,可以把该对象恢复到先前的状态,可以使用备忘录模式。
- 备忘录模式和原型模式组合使用
在发起人对象创建备忘录对象的时候,如果发起人对象中全部或者大部分的状态都需要保存,一个简洁的方式就是直接克隆一个发起人对象。也就是说,这个时候备忘录对象里存放的是一个发起人对象的实例。
二、实例
1、题目:
游戏进度:遇到大boss的时候都会保存一下进度:游戏角色的生命力,攻击力,防御力等的数据。如果:打过了就不需要恢复记录,打不过就复原到刚刚保存的记录重新打一遍boss,直到打死为止。
class GameRole{
int Vitality;//体力
int Attack;//攻击力
int Defense;//防御力
public int getVitality() {
return Vitality;
}
public void setVitality(int vitality) {
Vitality = vitality;
}
public int getAttack() {
return Attack;
}
public void setAttack(int attack) {
Attack = attack;
}
public int getDefense() {
return Defense;
}
public void setDefense(int defense) {
Defense = defense;
}
public void StateDisplay() {
System.out.println("角色当前状态");
System.out.println("体力:"+this.Vitality);
System.out.println("攻击力:"+this.Attack);
System.out.println("防御力:"+this.Defense);
}
public void SetInitState() {//初状态
this.Vitality=100;
this.Attack=100;
this.Defense=100;
}
public void Fight() {
this.Vitality=0;
this.Attack=0;
this.Defense=0;
}
}
public class Main{
public static void main(String[] args){
GameRole gr=new GameRole();
gr.SetInitState();
gr.StateDisplay();
//储存当前状态
GameRole grBackup=new GameRole();
grBackup.setVitality(gr.getVitality());
grBackup.setAttack(gr.getAttack());
grBackup.setDefense(gr.getDefense());
gr.Fight();
gr.StateDisplay();
//恢复之前状态
gr.setVitality(grBackup.getVitality());
gr.setAttack(grBackup.getAttack());
gr.setDefense(grBackup.getDefense());
gr.StateDisplay();
}
}
结果为: 角色当前状态 体力:100 攻击力:100 防御力:100 角色当前状态 体力:0 攻击力:0 防御力:0 角色当前状态 体力:100 攻击力:100 防御力:100
存在问题:
将所有的细节暴露给了客户端,导致客户端承担了太多的职责(保存状态,恢复状态,进行游戏),而且如果一旦游戏人物的属性修改或者添加了,那么客户端相关的代码也必须修改,这些代码紧紧地耦合在了一起。
解决方案:
并不一定需要把游戏角色这个类的所有局性都备份/存档,只需要把关心的数据进行存档,所以就要针对需要存档的数据进行封装,实现职责的分离。
这里,定义类RoleStateMemento封装需要存档的数据。
class GameRole{
int Vitality;//体力
int Attack;//攻击力
int Defense;//防御力
public int getVitality() {
return Vitality;
}
public void setVitality(int vitality) {
Vitality = vitality;
}
public int getAttack() {
return Attack;
}
public void setAttack(int attack) {
Attack = attack;
}
public int getDefense() {
return Defense;
}
public void setDefense(int defense) {
Defense = defense;
}
public void StateDisplay() {
System.out.println("角色当前状态");
System.out.println("体力:"+this.Vitality);
System.out.println("攻击力:"+this.Attack);
System.out.println("防御力:"+this.Defense);
}
public void SetInitState() {//初状态
this.Vitality=100;
this.Attack=100;
this.Defense=100;
}
public void Fight() {
this.Vitality=0;
this.Attack=0;
this.Defense=0;
}
public RoleStateMemento SaveRoleState() {//存档状态
return new RoleStateMemento(this.Vitality,this.Attack,this.Defense);
}
//从RoleStateMenento恢复状态
public void RecoveryState(RoleStateMemento memento) {
this.Vitality=memento.getVitality();
this.Attack=memento.getAttack();
this.Defense=memento.getDefense();
}
}
class RoleStateMemento{
int Vitality;//体力
int Attack;//攻击力
int Defense;//防御力
public int getVitality() {
return Vitality;
}
public void setVitality(int vitality) {
Vitality = vitality;
}
public int getAttack() {
return Attack;
}
public void setAttack(int attack) {
Attack = attack;
}
public int getDefense() {
return Defense;
}
public void setDefense(int defense) {
Defense = defense;
}
public RoleStateMemento(int vitality, int attack, int defense) {
super();
Vitality = vitality;
Attack = attack;
Defense = defense;
}
}
//Memento的管理者,负责保存Memento
class RoleStateCaretaker{
RoleStateMemento roleStateMemento;
public RoleStateMemento getRoleStateMemento() {
return roleStateMemento;
}
public void setRoleStateMemento(RoleStateMemento roleStateMemento) {
this.roleStateMemento = roleStateMemento;
}
}
public class Main{
public static void main(String[] args){
GameRole gr=new GameRole();
gr.SetInitState();
gr.StateDisplay();
//储存当前状态
RoleStateCaretaker caretaker=new RoleStateCaretaker();
caretaker.setRoleStateMemento(gr.SaveRoleState());
//进行游戏
gr.Fight();
gr.StateDisplay();
//恢复状态
gr.RecoveryState(caretaker.getRoleStateMemento());
gr.StateDisplay();
}
}
//结果为:
角色当前状态
体力:100
攻击力:100
防御力:100
角色当前状态
体力:0
攻击力:0
防御力:0
角色当前状态
体力:100
攻击力:100
防御力:100