备忘录模式(让对象返回之前的状态)

2017-11-19  本文已影响0人  钉某人
源码地址 https://github.com/DingMouRen/DesignPattern
备忘录模式.png
定义

备忘录模式在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样就可以将该对象恢复到原来保存的状态。

使用场景
举个例子

我们玩游戏的时候都有游戏存档的事情,基本流程:游戏开始--获取存档信息--开始游戏--退出游戏时进行存档,循环往复。

//备忘录类
public class Memoto {
    private int checkPoint;//关数
    private int lifeValue;//血量
    private String weapon;//武器

    public int getCheckPoint() {
        return checkPoint;
    }

    public void setCheckPoint(int checkPoint) {
        this.checkPoint = checkPoint;
    }

    public int getLifeValue() {
        return lifeValue;
    }

    public void setLifeValue(int lifeValue) {
        this.lifeValue = lifeValue;
    }

    public String getWeapon() {
        return weapon;
    }

    public void setWeapon(String weapon) {
        this.weapon = weapon;
    }
    @Override
    public String toString() {
        return "备忘录中存储的游戏关数:"+checkPoint+"  血量:"+lifeValue+" 武器:"+weapon;
    }
}

//相当于Originator,
public class Game {
    private int checkPoint = 1;//游戏第一关
    private int lifeValue = 100;//刚开始血量满满
    private String weapon = "匕首";//武器

    //开始游戏
    public void play(){
        System.out.println("开始游戏:"+String.format("第%d关",checkPoint)+"  fighting!! ︻$▅▆▇◤");
        lifeValue -= 10;
        System.out.println("闯关成功");
        checkPoint++;
        System.out.println("到达"+String.format("第%d关",checkPoint));
    }

    //退出游戏
    public void quit(){
        System.out.println(". . . . . .");
        System.out.println(this.toString()+"\n退出游戏");
    }

    //创建备忘录
    public Memoto createMemoto(){
        Memoto memoto = new Memoto();
        memoto.setCheckPoint(this.checkPoint);
        memoto.setLifeValue(lifeValue);
        memoto.setWeapon(weapon);
        return memoto;
    }

    //恢复游戏
    public void restore(Memoto memoto){
        this.checkPoint = memoto.getCheckPoint();
        this.lifeValue = memoto.getLifeValue();
        this.weapon = memoto.getWeapon();
        System.out.println("恢复后的游戏状态:"+this.toString());
    }

    @Override
    public String toString() {
        return "当前游戏关数:"+checkPoint+"  血量:"+lifeValue+" 武器:"+weapon;
    }
}
//管理者  管理备忘录
public class Caretaker {
    private Memoto memoto;//备忘录
    //存档
    public void saveMemoto(Memoto memoto){
        this.memoto = memoto;
    }
    //获取存档

    public Memoto getMemoto() {
        return memoto;
    }
}
使用
    public static void main(String[] args) {
        //构建游戏对象
        Game game = new Game();
        //构建管理者
        Caretaker caretaker =  new Caretaker();
        //开始游戏
        game.play();
        //游戏存档
        caretaker.saveMemoto(game.createMemoto());
        //退出游戏
        game.quit();
        //进入游戏时,恢复上一次游戏的状态
        Game newGame = new Game();
        System.out.println("登录游戏");
        newGame.restore(caretaker.getMemoto());
    }
总结

备忘录模式是在不破坏封装的条件下,通过备忘录对象Memoto存储另外一个对象内部状态的快照,在将来合适的时候把这个对象还原到存储的状态。

备忘录模式优点自然是为用户提供了一个可以恢复状态的机制,可以让用户方便的回到某个历史的状态,缺点就是在类成员变量变多的情况下,势必消耗资源,每一次保存都会消耗一定的内存。

上一篇下一篇

猜你喜欢

热点阅读