设计模式学习笔记——备忘录模式

备忘录模式

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

结构图


代码实现

备忘录类Memento:
/**
 * 备忘录:负责储存原始对象的内部状态
 * @author xukai
 * 2016年3月23日 下午6:44:26
 */
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;
	}
	
}
需要保存状态的对象:
/**
 * 保存对象:负责创建备忘录
 * @author xukai
 * 2016年3月23日 下午6:44:07
 */
public class Originator {

	private String state;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public Memento createMemento(){
		return new Memento(state);
	}
	
	public void setMemento(Memento memento){
		state = memento.getState();
	}
	
	public void show(){
		System.out.println("state=" + state);
	}
	
}
管理者:执行保存备忘录操作
/**
 * 管理者:保存备忘录
 * @author xukai
 * 2016年3月23日 下午10:55:32
 */
public class Caretaker {

	private Memento memento;

	/**
	 * @return
	 */
	public Memento getMemento() {
		return memento;
	}

	public void setMemento(Memento memento) {
		this.memento = memento;
	}
	
}
客户端:
public class Client {

	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();
	}
	
}
Console:
state=on
state=off
state=on
首先,创建一个初识对象,赋给其某种状态。
其次,使用管理者,执行保存备忘录操作,记录当前原始对象的状态。
接着,原始对象的状态由于某种原因发生改变,但是需要将状态恢复到保存备忘录的状态。
最后,原始对象通过管理者中的备忘录,获取保存的状态。

demo

问题:使用小霸王游戏机在打大BOSS之前一般都有存档,Game Over之后,可以回到存档位置,重新打BOSS。

结构图


代码实现

角色状态存储:
/**
 * 角色备忘录:角色状态存储
 * @author xukai
 * 2016年3月23日 下午11:17:56
 */
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;
	}
	
}
游戏角色:
/**
 * 游戏角色
 * @author xukai
 * 2016年3月23日 下午11:19:37
 */
public class GameCharacters {

	private int vit;	// 生命力
	
	private int atk;	// 攻击力
	
	private int def;	// 防御力
	
	// 初始化
	public void init(){
		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 recoveryState(RoleStateMemento roleStateMemento){
		this.vit = roleStateMemento.getVit();
		this.atk = roleStateMemento.getAtk();
		this.def = roleStateMemento.getDef();
	}

	@Override
	public String toString() {
		return "GameCharacters [vit=" + vit + ", atk=" + atk + ", def=" + def + "]";
	}
	
}
管理员:
/**
 * 管理员
 * 
 * @author xukai 2016年3月23日 下午11:23:14
 */
public class RoleStateCaretaker {

	private RoleStateMemento memento;

	public RoleStateMemento getMemento() {
		return memento;
	}

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

}
客户端测试:
public class Client {

	public static void main(String[] args) {
		// 准备打BOSS
		GameCharacters characters = new GameCharacters();
		characters.init();
		System.out.println(characters);
		
		// 保存进度
		RoleStateCaretaker caretaker = new RoleStateCaretaker();
		caretaker.setMemento(characters.saveState());
		
		// 打BOSS失败
		characters.fight();
		System.out.println(characters);
		
		//重新开始,读存档
		characters.recoveryState(caretaker.getMemento());
		System.out.println(characters);
	}
	
}
Console:
GameCharacters [vit=100, atk=100, def=100]
GameCharacters [vit=0, atk=0, def=0]
GameCharacters [vit=100, atk=100, def=100]

总结

Memento模式适用于比较复杂的、但需要维护或记录属性历史的类。或者需要保存的属性只是一小部分,Originator可以通过这些小部分信息还原到上一状态。
另外,适用备忘录可以将复杂对象的内部信息对其他对象屏蔽。
当角色的状态改变的时候,有可能这个状态无效,那么就可以使用备忘录暂时存储起来,方便状态复原。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值