1.16(設計模式)備忘錄模式

備忘錄模式,在不破壞對象封裝的狀況下,獲取對象狀態並將其保存下來。html

好比遊戲關卡的存檔。java

 

 

 

遊戲狀態類ide

經過saveStatefToMemento()方法存儲狀態this

public class Originator {
    //攻擊力
    private int aggressivity;
    //防護力
    private int defense;
    //關卡
    private int checkpoint;
    
    public Originator(int aggressivity, int defense, int checkpoint) {
        super();
        this.aggressivity = aggressivity;
        this.defense = defense;
        this.checkpoint = checkpoint;
    }

    public int getAggressivity() {
        return aggressivity;
    }

    public int getDefense() {
        return defense;
    }

    public int getCheckpoint() {
        return checkpoint;
    }
    
    public void setAggressivity(int aggressivity) {
        this.aggressivity = aggressivity;
    }

    public void setDefense(int defense) {
        this.defense = defense;
    }

    public void setCheckpoint(int checkpoint) {
        this.checkpoint = checkpoint;
    }

    public void setAll(int aggressivity, int defense, int checkpoint) {
        this.aggressivity = aggressivity;
        this.defense = defense;
        this.checkpoint = checkpoint;
    }
    //保存狀態 public Memento saveStatefToMemento() {//使用一個Memento類保存狀態,能夠保證Originator的封裝性 return new Memento(aggressivity, defense, checkpoint);
    }
    //獲取狀態 public void getStateFromMemento(Memento memento) {
        this.aggressivity = memento.getAggressivity();
        this.defense = memento.getDefense();
        this.checkpoint = memento.getCheckpoint();
    }
    
    @Override
    public String toString() {
        return "Originator [aggressivity=" + aggressivity + ", defense=" + defense + ", checkpoint=" + checkpoint + "]";
    }
    
}

 

 

保存遊戲狀態的類spa

該類的屬性時根據Originator 類的屬性設置的,code

經過該類保存遊戲狀態,能夠保證原有類的封裝性。htm

public class Memento {
    //攻擊力
    private int aggressivity;
    //防護力
    private int defense;
    //關卡
    private int checkpoint;
    public Memento(int aggressivity, int defense, int checkpoint) {
        super();
        this.aggressivity = aggressivity;
        this.defense = defense;
        this.checkpoint = checkpoint;
    }
    
    public int getAggressivity() {
        return aggressivity;
    }
    
    public int getDefense() {
        return defense;
    }
    
    public int getCheckpoint() {
        return checkpoint;
    }

}

 

 

獲取存儲狀態的類對象

import java.util.HashMap;
import java.util.Map;

public class CareTaker {
    private Map<String,Memento> mementosMap = new HashMap<String,Memento>();
    
    public void add(String keepInTheArchivesName,Memento memento) {
        mementosMap.put(keepInTheArchivesName, memento);
    }
    
    public Memento get(String keepInTheArchivesName) {
        return mementosMap.get(keepInTheArchivesName);
    }
}

 

Mainblog

public class Main {
    public static void main(String[] args) {
        Originator originator = new Originator(0, 0, 0);
        CareTaker careTaker = new CareTaker();
        
        originator.setAll(100,50,1);
        originator.setAll(200,70,2);
        careTaker.add("存檔一(已過第二關)",originator.saveStatefToMemento());
        originator.setAll(400,100,3);
        careTaker.add("存檔一(已過第三關)",originator.saveStatefToMemento());
        originator.setAll(500,150,4);
        
        System.out.println("current:" + originator);
        
        System.out.println("獲取存檔一狀態");
        originator.getStateFromMemento(careTaker.get("存檔一(已過第二關)"));
        System.out.println(originator);
        System.out.println("獲取存檔二狀態");
        originator.getStateFromMemento(careTaker.get("存檔一(已過第三關)"));
        System.out.println(originator);
        
    }
}
運行結果:
current:Originator [aggressivity=500, defense=150, checkpoint=4] 獲取存檔一狀態 Originator [aggressivity=200, defense=70, checkpoint=2] 獲取存檔二狀態 Originator [aggressivity=400, defense=100, checkpoint=3]

 

參考資料:遊戲

https://www.runoob.com/design-pattern/memento-pattern.html

相關文章
相關標籤/搜索