1. 程式人生 > >設計模式(十二):備忘錄模式

設計模式(十二):備忘錄模式

備忘錄模式:在不破壞封裝性的前提下,捕獲一個物件的內部狀態,並在該物件之外儲存這個狀態。這樣以後就可將該物件恢復到原先儲存的狀態。


(1):Originator:負責例項化一個備忘錄Memento類,記錄當前時刻它的內部狀態,作為備份。

(2):Memento:備忘錄類,Originator的狀態備份

(3)Caretaker:管理者類,管理備忘錄類,通過caretaker對備忘錄設定和訪問

例子:

玩仙劍的時候,每次大戰boss之前,一般都會有一個存檔的地方儲存遊戲的當前進度,如果大戰boss失敗,可以讀取存檔重新來過,存檔就相當於備忘錄。

1、Originator類,遊戲角色

(1)屬性:包含生命力,防禦力、攻擊力三個屬性,初始化值都為100

(2)方法:saveState()方法,儲存當前的狀態,並返回一個備忘錄類,相當於遊戲中的存檔 

                    recoverState(Memento),恢復狀態方法,需要傳入一個備忘錄類,從備忘錄中恢復狀態

                    show()方法,顯示當前狀態

                    attack()方法       

/**
 * Originator類
 */
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 show(){
        System.out.println("生命力:"+vitality);
        System.out.println("攻擊力:"+attack);
        System.out.println("防禦力:"+defense);
    }

    /**
     * 儲存狀態
     * @return
     */
    public RoleStateMemento saveState(){
        //返回一個備忘錄類,用來儲存當前狀態
        return new RoleStateMemento(this.vitality,this.attack,this.defense);
    }

    /**
     * 恢復狀態
     * @param roleStateMemento
     */
    public void recoveryState(RoleStateMemento roleStateMemento){
        this.vitality=roleStateMemento.getVitality();
        this.defense=roleStateMemento.getDefense();
        this.attack=roleStateMemento.getAttack();
    }

    /**
     * 戰鬥
     */
    public void fight(){
        this.vitality=0;
        this.attack=0;
        this.defense=0;
    }

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

2、備忘錄類,用來儲存Originator的狀態
/**
 * Memento備忘錄類
 */
public class RoleStateMemento {
    private int vitality;//生命力
    private int attack;//攻擊力
    private int defense;//防禦力

    public RoleStateMemento(int vitality, int attack, int defense) {
        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;
    }
}

3、管理者類

包含一個對備忘錄類的引用,負責設定和訪問備忘錄類

/**
 * Caretaker管理者類
 */
public class RoleStateCaretaker {
    private RoleStateMemento memento;

    public RoleStateMemento getMemento() {
        return memento;
    }

    public void setMemento(RoleStateMemento memento) {
        this.memento = memento;
    }
}
4、客戶端
public class Test {
    public static void main(String[] args) {
        //建立遊戲角色李逍遙
        GameRole lixiaoyao=new GameRole();
        System.out.println("戰鬥前:");
        lixiaoyao.show();

        //儲存進度,建立一個管理者
        RoleStateCaretaker roleStateCaretaker=new RoleStateCaretaker();
        roleStateCaretaker.setMemento(lixiaoyao.saveState());//由管理者管理備忘錄類

        //戰鬥
        lixiaoyao.fight();
        System.out.println("戰鬥後:");
        lixiaoyao.show();

        //恢復進度
        lixiaoyao.recoveryState(roleStateCaretaker.getMemento());
        System.out.println("恢復:");
        lixiaoyao.show();
    }
}

輸出:
戰鬥前:
生命力:100
攻擊力:100
防禦力:100
戰鬥後:
生命力:0
攻擊力:0
防禦力:0
恢復:
生命力:100
攻擊力:100
防禦力:100

來自:大話設計模式