设计模式之备忘录模式

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

来看看备忘录模式的基本代码

/**
 * 
 * @author ricardo
 * @Time 下午8:07:41
 * @Function:
 *		备忘录模式
 */
public class Origunator {
	private String state;
	
	//建立状态存储对象
	public Memento createMemento() {
		return new Memento(state);
	}
	//设置状态存储对象
	public void setMemento(Memento memento) {
		state = memento.getState();
	}
	//展示状态
	public void show() {
		System.out.println("状态为:" + state);
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}	
}
/**
 * 
 * @author ricardo
 * @Time 下午8:14:16
 * @Function:备忘录
 *
 */
public class Memento {	
	private String state;	
	public Memento(String state) {
		this.state = state;
	}
	public String getState() {
		return state;
	}
}
/**
 * 
 * @author ricardo
 * @Time 下午8:15:02
 * @Function:管理者
 *
 */
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) {
		Origunator origunator = new Origunator();
		origunator.setState("begin");
		origunator.show();
		
		Caretaker caretaker = new Caretaker();
		caretaker.setMemento(origunator.createMemento());
		
		origunator.setState("Game over");
		origunator.show();
		
		origunator.setMemento(caretaker.getMemento());
		origunator.show();
	}
}
运行截图



打游戏的时候经常会看到存档的操作,这是为了备份游戏数据,及时有断电或者其他的意外情况,都能恢复游戏进度什么的,我们来看一个案例。

/**
 * 
 * @author ricardo
 * @Time 下午8:21:31
 * @Function:游戏角色信息
 *
 */
public class GameRole {
	//角色坐标
	private int level;
	//角色地图坐标
	private int coordinate;
	//章节进度
	private int chapter;
	//生命值
	private int HP;
	//法力值
	private int MP;
	
	//保存状态
	public GameState saveState() {
		return new GameState(level,coordinate,chapter,HP,MP);
	}
	//覆盖存档
	public void reCover(GameState gameState) {
		this.level = gameState.getLevel();
		this.chapter = gameState.getChapter();
		this.coordinate =  gameState.getCoordinate();
		this.HP = gameState.getHP();
		this.MP = gameState.getMP();
	}
	//显示存档的状态
	public void showState() {
		System.out.println("*****************************************");
		System.out.println("进度信息如下:");
		System.out.println("角色的级别为" + this.level);
		System.out.println("角色的坐标为" + this.coordinate);
		System.out.println("角色的生命值为" + this.HP);
		System.out.println("角色的法力值为" + this.MP);
		System.out.println("当前进度为第:" + this.chapter + "章节" );
		System.out.println("*****************************************");		
	}
	
	//初始化人物状态
	public void init() {
		this.level = 10;
		this.chapter = 5;
		this.coordinate = 255;
		this.HP = 1433;
		this.MP = 681;
	}
	//完成任务后的状态
	public void doTask() {
		this.level = 15;
		this.chapter = 6;
		this.coordinate = 999;
		this.HP = 433;
		this.MP = 100;
	}
}
/**
 * 
 * @author ricardo
 * @Time 下午8:46:14
 * @Function:备忘录
 *
 */
public class GameState {
	//角色坐标
	private int level;
	//角色地图坐标
	private int coordinate;
	//章节进度
	private int chapter;
	//生命值
	private int HP;
	//法力值
	private int MP;
	public GameState(int level, int coordinate, int chapter, int hP, int mP) {
		super();
		this.level = level;
		this.coordinate = coordinate;
		this.chapter = chapter;
		HP = hP;
		MP = mP;
	}
	public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getCoordinate() {
		return coordinate;
	}
	public void setCoordinate(int coordinate) {
		this.coordinate = coordinate;
	}
	public int getChapter() {
		return chapter;
	}
	public void setChapter(int chapter) {
		this.chapter = chapter;
	}
	public int getHP() {
		return HP;
	}
	public void setHP(int hP) {
		HP = hP;
	}
	public int getMP() {
		return MP;
	}
	public void setMP(int mP) {
		MP = mP;
	}
	
}
/**
 * 
 * @author ricardo
 * @Time 下午8:46:38
 * @Function:存档管理
 *
 */
public class GameManager {
	private GameState gameState;

	public GameState getGameState() {
		return gameState;
	}

	public void setGameState(GameState gameState) {
		this.gameState = gameState;
	}	
}
客户端

public class Client {
	public static void main(String[] args) {
		//游戏初始状态
		GameRole first = new GameRole();
		first.init();
		System.out.println("任务之前");
		first.showState();
		
		//进度保存
		GameManager gameManager = new GameManager();
		gameManager.setGameState(first.saveState());
		
		//任务失败
		System.out.println("任务失败");
		first.doTask();
		first.showState();
		
		//归档
		first.reCover(gameManager.getGameState());
		System.out.println("归档后");
		first.showState();
	}
}
运行截图



备忘录模式可以使开发人员捕获到一个对象的内部状态,并在以后某个时刻恢复一些相关的数据,存储的措施主要依赖于恢复时间的间隔。如果开发人员在一个应用程序运行期间保存并恢复了某个对象,那么这种行为的原因一般是为取消操作提供一些支持。

以上内容,整理自刘径舟,张玉华编著的《设计模式其实很简单》读书笔记,欢迎转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值