此文章是vip文章,如何查看?  

1,点击链接获取密钥 http://nicethemes.cn/product/view29882.html

2,在下方输入文章查看密钥即可立即查看当前vip文章


设计模式----备忘录模式(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;
	}
}

本文链接http://element-ui.cn/news/show-366810.aspx