其他分享
首页 > 其他分享> > 18.备忘录模式

18.备忘录模式

作者:互联网

package memento;
/*
 * :以前的小霸王游戏是不是可以保存游戏进度?
 * :是的
 * :那是怎么实现的呢?
 * :应该是...
 * :来 先简单实现一下  
 * 
 */
public class Commonly {
	public static void main(String[] args) {
		GameRole gameRole = new GameRole();
		gameRole.GetInitState();
		gameRole.Display();
		
		//备份
		GameRole gameRole2 = new GameRole();
		gameRole2.setVit(gameRole.getVit());
		gameRole2.setAtk(gameRole.getAtk());
		gameRole2.setDef(gameRole.getDef());
		
		gameRole.Fight();
		gameRole.Display();
		
		//恢复状态
		gameRole.setVit(gameRole2.getVit());
		gameRole.setAtk(gameRole2.getAtk());
		gameRole.setDef(gameRole2.getDef());
		
		gameRole.Display();
		
		
		
		/*
		 * 把属性给用户开放了 不是很好的选择   代码未错未必优
		 */
	}
}

class GameRole{
	//生命力
	private int vit;
	//***力
	private int atk;
	//防御力
	private int def;
	
	/*
	 * 展示角色信息
	 */
	public void Display(){
		System.out.println("角色当前状态:");
		System.out.println("体力:"+vit);
		System.out.println("***力"+atk);
		System.out.println("防御力"+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 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;
	}
	
	
}
package memento;
/*
 * 备忘录模式:
 *  在不破坏封装性的前提下,捕获一个对象的内部状态
 *  并在该对象之外保存这个状态 这样以后就可以将该对象恢复到原有保存的状态
 *  
 *  
 *  :
 *  发起人
 *  备忘录
 *  管理者
 */
public class Memento {
	public static void main(String[] args) {
		Role role = new Role();
		role.GetInitState();
		//展示信息
		role.Display();
		//备份
		RoleState roleState = role.RoleStateMemento();
		//战斗
		role.Fight();
		role.Display();
		
		//恢复
		
		role.RoleStateMemento(roleState);
		role.Display();
		
		/*
		 * 很明显这里的三个属性没有给调用者开放 
		 * 这样不管是备份还是恢复都很方便  
		 */
		
		
	}
}

class Role{
	//生命力
	private int vit;
	//***力
	private int atk;
	//防御力
	private int def;
	
	
	
	
	/*
	 * 保存游戏角色属性
	 */
	public RoleState RoleStateMemento(){
		return  new RoleState(vit,atk,def);
	}
	/*
	 * 恢复游戏角色
	 */
	public void RoleStateMemento(RoleState roleState ){
		this.vit = roleState.getVit();
		this.atk = roleState.getAtk();
		this.def = roleState.getDef();
	}
	/*
	 * 展示角色信息
	 */
	public void Display(){
		System.out.println("角色当前状态:");
		System.out.println("体力:"+vit);
		System.out.println("***力"+atk);
		System.out.println("防御力"+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 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;
	}
	
}


/*
 * 角色状态类(专门存角色的各种属性)
 */
class RoleState{
	public RoleState(int vit,int atk,int def){
		this.vit = vit;
		this.atk = atk;
		this.def = def;
	}
	//生命力
	private int vit;
	//***力
	private int atk;
	//防御力
	private int 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;
	}
	
}



标签:int,18,void,atk,模式,备忘录,vit,public,def
来源: https://blog.51cto.com/u_12198094/2700015