备忘录模式

《大话设计模式》第18章读书笔记,介绍备忘录模式

Posted by GrayWind on November 17, 2018

备忘录模式(Memento)在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。

memento

发起人类,负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的哪些内部状态。

public class Originator {
	//需要保存的属性,可能有多个
	private String state;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	
	//创建备忘录,将当前需要保存的信息导入并实例化出一个Memento对象
	public Memento createMemento() {
		return new Memento(state);
	}
	
	//恢复备忘录,将Memento导入并将相关数据恢复
	public void setMemento(Memento memento) {
		state = memento.getState();
	}
	
	//显示数据
	public void show() {
		System.out.println("state=" + state);
	}
}

备忘录类,负责存储Originator对象的内部状态,并可防止Originator以外的其他对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。

public class Memento {
	private String state;

	//构造方法,将相关数据导入
	public Memento(String state) {
		this.state = state;
	}

	//需要保存的数据属性,可以是多个
	public String getState() {
		return state;
	}
}

管理者类,负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查

public class Caretaker {
	private Memento memento;

	//得到备忘录
	public Memento getMemento() {
		return memento;
	}

	//设置备忘录
	public void setMemento(Memento memento) {
		this.memento = memento;
	}
}

客户端代码

public class Test {
	public static void main(String[] args) {
		Originator o = new Originator();
		o.setState("On");//初始状态,状态为On
		o.show();//state=On
		
		Caretaker c = new Caretaker();
		c.setMemento(o.createMemento());//保存状态时,由于有了很好的封装,可以隐藏Originator的实现细节
		
		o.setState("Off");//Originator改变状态为Off
		o.show();//state=Off
		
		o.setMemento(c.getMemento());//恢复初始状态
		o.show();//state=On
	}
}

备忘录模式就是要把保存的细节给封装在Memento中,要改变保存细节不需要影响客户端。适用于功能比较复杂,但是需要维护或者记录属性历史的类,或者需要保存的属性只是众多属性的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来存储可撤销操作的状态。有时一些对象的内部信息必须保存在对象以外的地方,但是必须要由对象自己读取,这时,使用备忘录可以把复杂的对象内部信息对其他的对象屏蔽起来,从而恰当地保持封装的边界。最大的作用还是当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。

以游戏中人物数据的存档和读取为例,现在有一个游戏角色类,作为发起人,可以创建备忘录并通过备忘录进行恢复数据

public class GameRole {
	private int vit;
	private int atk;
	private int def;
	
	//获得初始状态
	public void getInitState() {
		this.vit = 100;
		this.atk = 100;
		this.def = 100;
	}
	
	//战斗
	public void fight() {
		this.vit = 0;
		this.atk = 0;
		this.def = 0;
	}
	
	public void stateDisplay() {
		System.out.printf("角色当前状态:\n");
		System.out.printf("体力:%d\n", this.vit);
		System.out.printf("攻击力:%d\n", this.atk);
		System.out.printf("防御力:%d\n", this.def);
		System.out.println();
	}

	// 保存角色状态
	public RoleStateMemento saveState() {
		return new RoleStateMemento(vit, atk, def);
	}
	
	//恢复角色状态,可将外部的角色状态存储箱中的状态值恢复给游戏角色
	public void recoveryState(RoleStateMemento memento) {
		this.vit = memento.getVit();
		this.atk = memento.getAtk();
		this.def = memento.getDef();
	}
}

RoleStateMemento是备忘录类,需要存储三个属性

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;
	}
}

RoleStateCaretaker管理者类只负责保存备忘录

public class RoleStateCaretaker {
	private RoleStateMemento memento;

	public RoleStateMemento getMemento() {
		return memento;
	}

	public void setMemento(RoleStateMemento memento) {
		this.memento = memento;
	}
}

客户端代码如下

public class GameRoleTest {
	public static void main(String[] args) {
		//大战Boss前
        GameRole lixiaoyao = new GameRole();
        lixiaoyao.getInitState();
        lixiaoyao.stateDisplay();

        //保存进度
        RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
        stateAdmin.setMemento(lixiaoyao.saveState());

        //大战Boss时,损耗严重
        lixiaoyao.fight();
        lixiaoyao.stateDisplay();

        //恢复之前状态
        lixiaoyao.recoveryState(stateAdmin.getMemento());

        lixiaoyao.stateDisplay();
	}
}