备忘录模式

来源:互联网 发布:小学生数学出题软件 编辑:程序博客网 时间:2024/05/16 07:55

一句话定义

在对象外保存对象的状态,且不破坏封闭,方便恢复保存状态。

使用场景

1. 需要保存对象状态,方便恢复2. 通过中间对象间接的提供内部状态的访问

实现要点

1.  提供中间实体类,只负责储存数据

情景假设

游戏状态包括生命、魔法、关卡三种状态,用户退出进行保存,下次游戏读取存档。

实现步骤

1. 创建备忘录,只保存数据。
/** * 游戏备忘录 * 生命、魔法、关卡 三个属性数据 * * @author fengzhen * @version 1.0, 2017/2/10 */public class GameMemento {    // 生命值    private int healthPoint;    // 魔法值    private int magicPoint;    // 关卡    private int round;    public GameMemento(int healthPoint, int magicPoint, int round) {        this.healthPoint = healthPoint;        this.magicPoint = magicPoint;        this.round = round;    }    public int getHealthPoint() {        return healthPoint;    }    public void setHealthPoint(int healthPoint) {        this.healthPoint = healthPoint;    }    public int getMagicPoint() {        return magicPoint;    }    public void setMagicPoint(int magicPoint) {        this.magicPoint = magicPoint;    }    public int getRound() {        return round;    }    public void setRound(int round) {        this.round = round;    }}
2. 创建发起者,提供恢复存档和创建存档方法。
/** * 游戏类 * 备忘发起者,包含储存、恢复方法 * * @author fengzhen * @version 1.0, 2017/2/10 */public class GameOriginator {    // 生命值    private int healthPoint = 100;    // 魔法值    private int magicPoint = 100;    // 关卡    private int round = 1;    /**     * 进行游戏状态改变     */    public void play(){        healthPoint = 56;        magicPoint = 88;        round = 3;    }    /**     * 创建备忘录,开始存储     */    public GameMemento createMememto(){        return new GameMemento(healthPoint, magicPoint, round);    }    /**     * 恢复存档     * @param memento 保存的存档     */    public void restore(GameMemento memento){        healthPoint = memento.getHealthPoint();        magicPoint = memento.getMagicPoint();        round = memento.getRound();    }    @Override    public String toString() {        return "GameOriginator{" +                "healthPoint=" + healthPoint +                ", magicPoint=" + magicPoint +                ", round=" + round +                '}';    }}
3. 创建备忘录托管,存取备忘录方法。
/** * 备忘录托管者 * 对备忘录进行存储管理 * * @author fengzhen * @version 1.0, 2017/2/10 */public class Caretaker {    private List<GameMemento> mementos = new ArrayList<>();    /**     * 加入新的备忘录     *     * @param memento 新的备忘录     */    public void add(GameMemento memento) {        mementos.add(memento);    }    /**     * 获取备忘录     *     * @param index 索引,倒叙     * @return 索引对应的备忘录     */    public GameMemento getMemento(int index) {        // 前一个状态        int i = mementos.size() - index;        if (i >= 1 && i <= mementos.size()) {            return mementos.get(i-1);        }        return null;    }}
4. 具体使用
        //备忘录模式        GameOriginator game = new GameOriginator();        Log.i("info", "onCreate: ==++" + game.toString());        game.play();        Log.i("info", "onCreate: ==++" + game.toString());        // 存档        Caretaker caretaker = new Caretaker();        caretaker.add(game.createMememto());        // 退出   下一次进入 读档        GameOriginator newGame = new GameOriginator();        newGame.restore(caretaker.getMemento(0));        Log.i("info", "onCreate: ==++" + newGame.toString());
1 0
原创粉丝点击