1.16(设计模式)备忘录模式

备忘录模式,在不破坏对象封装的情况下,获取对象状态并将其保存下来。

比如游戏关卡的存档。

 

 

 

游戏状态类

通过saveStatefToMemento()方法存储状态

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 + "]";
    }
    
}

 

 

保存游戏状态的类

该类的属性时根据Originator 类的属性设置的,

通过该类保存游戏状态,可以保证原有类的封装性。

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 mementosMap = new HashMap();
    
    public void add(String keepInTheArchivesName,Memento memento) {
        mementosMap.put(keepInTheArchivesName, memento);
    }
    
    public Memento get(String keepInTheArchivesName) {
        return mementosMap.get(keepInTheArchivesName);
    }
}

 

Main

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

 

你可能感兴趣的:(1.16(设计模式)备忘录模式)