设计模式----备忘录模式(JAVA)

模式简析

/**
 * 
 */
/**
 * 备忘录模式
 * 动机:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。
 * 解决的问题:所谓备忘录模式就是在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,
 * 这样可以在以后将对象恢复到原先保存的状态。
 * 优点: 1、给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。
 * 2、实现了信息的封装,使得用户不需要关心状态的保存细节。
 * 缺点:消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。
 * 使用场景: 
 * 1、需要保存/恢复数据的相关状态场景。 
 * 2、提供一个可回滚的操作。
 * 注意: 1、为了符合迪米特原则,还要增加一个管理备忘录的类。 2、为了节约内存,可使用原型模式+备忘录模式。 
 * 
 * 
 * 
 * 
 * @author 
 *
 */

具体实例

/**
 * 游戏进度保存
 * 失败后可以从保存的位置继续游戏
 *
 */
public class Demo1 {
	public static void main(String[] args) {
		GameRole g1 = new GameRole();
		g1.GetInitState();
		GameRole g2 = new GameRole();
		g2.setVitality(g1.getVitality());
		g2.setAttack(g1.getAttack());
		g2.setDefense(g1.getDefense());
		g1.DisplayState();
		g1.Fight();
		g1.DisplayState();
		System.out.println("回复保存时的状态");
		g1.setVitality(g2.getVitality());
		g1.setAttack(g2.getAttack());
		g1.setDefense(g2.getDefense());
		g1.DisplayState();
	}
}
class GameRole{
	private int vitality;
	private int attack;
	private int defense;
	public int getVitality() {
		return vitality;
	}
	public void setVitality(int vitality) {
		this.vitality = vitality;
	}
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	public void DisplayState() {
		System.out.println("游戏角色当前状态:\n");
		System.out.println("生命力:"+vitality+"\n");
		System.out.println("攻击力:"+attack+"\n");
		System.out.println("防御力:"+defense+"\n");
	}
	public void GetInitState() {
		this.vitality=100;
		this.attack=100;
		this.defense=100;
	}
	public void Fight() {
		this.vitality=0;
		this.attack=0;
		this.defense=0;
	}
}
/*
 * 存在的问题
 * 将所有细节都暴露给了客户端,客户端承担了太多责任
 * 代码是紧耦合在一起
 */
/*
 * 备忘录模式基本代码
 */
public class Demo2 {
	public static void main(String[] args) {
		Originator o = new Originator();
		o.setState("状态1");
		o.showState();
		
		Caretaker c =new Caretaker();
		c.setMemento(o.createMemento());
		
		o.setState("状态2");
		o.showState();
		
		o.setMemenot(c.getMemento());
		o.showState();
	}
}
//备忘录
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;
	}
	
}
//创建者
class Originator{
	private String state;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	public Memento createMemento(){
		return new Memento(this.state);
	}
	public void setMemenot(Memento memento) {
		state = memento.getState();
	}
	public void showState() {
		System.out.println("当前状态:"+state);
	}
}
class Caretaker{
	private Memento memento;

	public Memento getMemento() {
		return memento;
	}

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


在这里插入图片描述

/**
 * 游戏进度保存----备忘录模式实现
 * 失败后可以从保存的位置继续游戏
 *
 */
public class Demo1 {
	public static void main(String[] args) {
		GameRole g1 = new GameRole();
		g1.GetInitState();
		g1.DisplayState();
		
		RoleCaretaker rc =new RoleCaretaker();
		rc.setRm(g1.SaveState());
		
		g1.Fight();
		g1.DisplayState();
		
		g1.RecoverState(rc.getRm());
		g1.DisplayState();
		
}
}
class GameRole{
	private int vitality;
	private int attack;
	private int defense;
	public int getVitality() {
		return vitality;
	}
	public void setVitality(int vitality) {
		this.vitality = vitality;
	}
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	public void DisplayState() {
		System.out.println("游戏角色当前状态:\n");
		System.out.println("生命力:"+vitality+"\n");
		System.out.println("攻击力:"+attack+"\n");
		System.out.println("防御力:"+defense+"\n");
	}
	public void GetInitState() {
		this.vitality=100;
		this.attack=100;
		this.defense=100;
	}
	public void Fight() {
		this.vitality=0;
		this.attack=0;
		this.defense=0;
	}
	public RoleMemento SaveState() {
		return new RoleMemento(vitality, attack, defense);
	}
	public void RecoverState(RoleMemento rm) {
		this.vitality=rm.getVitality();
		this.attack=rm.getAttack();
		this.defense=rm.getDefense();
	}
}
class RoleMemento{
	private int vitality;
	private int attack;
	private int defense;
	public RoleMemento(int vitality, int attack, int defense) {
		super();
		this.vitality = vitality;
		this.attack = attack;
		this.defense = defense;
	}
	public int getVitality() {
		return vitality;
	}
	public void setVitality(int vitality) {
		this.vitality = vitality;
	}
	public int getAttack() {
		return attack;
	}
	public void setAttack(int attack) {
		this.attack = attack;
	}
	public int getDefense() {
		return defense;
	}
	public void setDefense(int defense) {
		this.defense = defense;
	}
	
}

class RoleCaretaker{
	private RoleMemento rm;

	public RoleMemento getRm() {
		return rm;
	}

	public void setRm(RoleMemento rm) {
		this.rm = rm;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值