java 备忘录模式(大话设计模式)

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

在这里插入图片描述

/**
 * 备忘录(Memento)类
 * 
 * @author administrator
 *
 */
public 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;
    }

}
/**
 * 发起人(Originator) 类
 * 
 * @author administrator
 *
 */
public class Originator {

    private String state;

    public Memento createMemento() {
	return new Memento(this.state);
    }

    public void recoverMemento(Memento memento) {
	this.setState(memento.getState());
    }

    public void show() {
	System.out.println("state = " + this.state);
    }

    public String getState() {
	return state;
    }

    public void setState(String state) {
	this.state = state;
    }

}
/**
 * 管理者(CareTaker)类:管理备忘录
 * 
 * @author administrator
 *
 */
public class CareTaker {

    private Memento memento;

    public Memento getMemento() {
	return memento;
    }

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

}
/**
 * 客户端
 * 
 * @author administrator
 *
 */
public class MementoClient {

    public static void main(String[] args) {
	// 设置初始状态
	Originator originator = new Originator();
	originator.setState("On");
	originator.show();

	// 管理者通过备忘录保存状态,由于有了很好地封装,可以隐藏Originator的实现细节
	CareTaker careTaker = new CareTaker();
	careTaker.setMemento(originator.createMemento());

	// 改变状态
	originator.setState("Off");
	originator.show();

	// 通过管理者从备忘录中恢复状态
	originator.recoverMemento(careTaker.getMemento());
	originator.show();
    }

}

案例 游戏


/**
 * 备忘录模式(Memento)
 * 游戏角色
 */
public class GameRole {

    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;
    }

    // 状态显示
    public void stateDisplay() {
        System.out.println("角色当前状态:");
        System.out.println("体力:" + this.vit);
        System.out.println("体力:" + this.atk);
        System.out.println("体力:" + this.def);
        System.out.println(" ");
    }

    // 获得初始状态
    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 RoleStateMemento saveState() {
        return new RoleStateMemento(vit, atk, def);
    }

    // 恢复角色状态
    public void recoveryState(RoleStateMemento memento) {
        this.vit = memento.getVit();
        this.atk = memento.getAtk();
        this.def = memento.getDef();
    }

}
/**
 * 备忘录模式(Memento)
 * 游戏角色状态存储箱
 */
public class RoleStateMemento {

    private int vit;
    private int atk;
    private int def;

    public RoleStateMemento(int vit, int atk, int def) {
        this.vit = vit;
        this.atk = atk;
        this.def = 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;
    }

}
/**
 * 备忘录模式(Memento)
 * 游戏角色状态管理者
 */
public class RoleStateCaretaker {

    private RoleStateMemento memento;

    public RoleStateMemento getMemento() {
        return memento;
    }

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

}
/**
 * 备忘录模式(Memento)
 * 客户端main方法
 */
public class Client {

    public static void main(String[] args) {
        // 打Boss前
        GameRole lixiaoyao = new GameRole();
        lixiaoyao.getInitState();
        lixiaoyao.stateDisplay();
        // 保存进度
        RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
        stateAdmin.setMemento(lixiaoyao.saveState());
        // 打Boss时,GameOver了
        lixiaoyao.fight();
        lixiaoyao.stateDisplay();
        // 恢复之前状态
        lixiaoyao.recoveryState(stateAdmin.getMemento());
        lixiaoyao.stateDisplay();
    }

}

猜你喜欢

转载自blog.csdn.net/imxlw00/article/details/86609592