设计模式读书笔记之备忘录模式(Memento)

备忘录模式:在不破坏封装性的前提下,捕获对象的内部状态并保存,这样以后就可以恢复该对象恢复到保存的状态。
设计模式读书笔记之备忘录模式(Memento)

 备忘录模式比较简单, 不要因为名字比较陌生而不敢学它. 它没有复杂的结构, 上图已经能足够说明问题.

Originator: 备忘录发起人, 通常是需要备忘的对象自己.

Memento: 备忘录对象, 保存了Originator的内部状态.

CareTaker: 备忘录管理者.

//备忘录发起人,即需要备忘的对象
package designpattern.memento;
public class Originator {
	private String state;

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}
	public Memento createMemo(){
		return new Memento(state);
	}
	public void recover(Memento m){
		this.state = m.getState();
	}
	public void show(){
		System.out.println("state = " + state);
	}
}
//备忘录
package designpattern.memento;

public class Memento {
	private String state;

	public String getState() {
		return state;
	}
	public Memento(String state){
		this.state = state;
	}
}
//备忘录管理者
package designpattern.memento;
public class CareTaker {
	private Memento memento;
	public Memento getMemento() {
		return memento;
	}

	public void setMemento(Memento memento) {
		this.memento = memento;
	}
	
}
//test case
package designpattern.memento;
public class Test {
	public static void main(String[] args) {
		Originator o = new Originator();
		o.setState("On");
		o.show();
		CareTaker c = new CareTaker();
		c.setMemento(o.createMemo());
		o.setState("Off");
		o.show();
		o.recover(c.getMemento());
		o.show();
	}
}

举例:

打rpg游戏的时候经常在打大boss之前把游戏保存一下,如果死了,就重新来。其实这就是备忘录模式。看代码:

package designpattern.memento.game;

public class GameRole {
	private int vitality;
	private int attack;
	private int defense;
	
	public GameRole(){
		this.vitality = 100;
		this.attack = 100;
		this.defense = 100;
	}
	public void showState(){
		System.out.println("vitality = " + vitality + "\nattack = " + attack + "\ndefence = " + defense);
	}
	public RoleStateMemo save(){
		return new RoleStateMemo(vitality, attack, defense);
	}
	public void recover(RoleStateMemo m){
		System.out.println("太菜了我,还好我存盘了,哈哈。");
		System.out.println("复活中......");
		this.vitality = m.getVitality();
		this.attack = m.getAttack();
		this.defense = m.getDefense();
		System.out.println("又复活了.");
	}
	public void fight(){
		System.out.println("与大Boss决战.\n......");
		System.out.println("你输了, 你的生命,防御, 战斗力都将减为0.");
		this.vitality = 0;
		this.defense = 0;
		this.attack = 0;
	}
}
//
package designpattern.memento.game;

public class RoleStateMemo {
	private int vitality;
	private int attack;
	private int defense;
	public RoleStateMemo(int v, int a, int d){
		this.vitality = v;
		this.attack = a;
		this.defense = d;
	}
	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;
	}
	
}
//
package designpattern.memento.game;

public class RoleMemoManager {
	private RoleStateMemo m;
	public RoleStateMemo getM() {
		return m;
	}
	public void setM(RoleStateMemo m) {
		this.m = m;
	}
}
//test case
package designpattern.memento.game;
public class Test {
	public static void main(String[] args) {
		GameRole role = new GameRole();
		role.showState();
		RoleMemoManager manager = new RoleMemoManager();
		manager.setM(role.save());
		role.fight();
		role.showState();
		role.recover(manager.getM());
		role.showState();
	}
}

适用情景:

需要维护或记录历史的类。

你可能感兴趣的:(设计模式,游戏,C++,c,读书)