大话设计模式-第18章 如果再回到从前--备忘录模式- 高飞网

第18章 如果再回到从前--备忘录模式

2016-02-02 17:05:22.0

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


//发起人,负责保存内部状态,恢复内部状态
public class Originator{
        private String state;//状态

        public void setState(String state){
                this.state = state;
        }

        //显示状态
        public void displayState(){
                System.out.println("当前角色状态"+state);
        }

        //保存状态
        public Memento saveState(){
                return new Memento(state);
        }

        //恢复状态
        public void recoveryState(Memento m){
                this.state = m.getState();
        }
}
public class Memento{
        private String state;

        public Memento(String state){
                this.state = state;
        }
        public String getState(){
                return this.state;
        }
}
public class Caretaker{
        private Memento memento;

        public void setMemento(Memento memento){
                this.memento = memento;
        }
        public Memento getMemento(){
                return this.memento;           
        }
}
public class App{
        public static void main(String[] args){
                Originator orig = new Originator();
                orig.setState("on");//初始化状态
                System.out.println("初始化状态:");
                orig.displayState();

                Memento memento = orig.saveState();//保存状态
                Caretaker caretaker = new Caretaker();
                caretaker.setMemento(memento);//由管理者管理状态
               
                orig.setState("off");
                System.out.println("改后的状态:");
                orig.displayState();

                orig.recoveryState(caretaker.getMemento());
                System.out.println("恢复后状态:");
                orig.displayState();

        }
}


游戏保存状态的实例: 


//游戏角色类
public class GameRole{
    private int vit;//生命力
    private int atk;//攻击力
    private int def;//防御力

    //初始化状态
    public void initState(){
        this.vit = 100;
        this.atk = 100;
        this.def = 100;
    }

    //显示状态
    public void displayState(){
        System.out.println("当前角色状态:");
        System.out.println("生命力:"+vit);
        System.out.println("攻击力:"+atk);
        System.out.println("防御力:"+def);
    }

    //攻击
    public void fight(){
        this.vit = 0;
        this.atk = 0;
        this.def = 0;
    }

    //保存状态
    public GameRoleStateMemento saveState(){
        return new GameRoleStateMemento(vit,atk,def);
    }

    //恢复状态
    public void recoveryState(GameRoleStateMemento g){
        this.vit = g.getVit();
        this.atk = g.getAtk();
        this.def = g.getDef();
    }
}
public class GameRoleStateMemento{
   
    private int vit;//生命力
    private int atk;//攻击力
    private int def;//防御力
   
    //初始化状态
    public GameRoleStateMemento(int vit,int atk,int def){
        this.vit = vit;
        this.atk = atk;
        this.def = def;
    }

    public int getVit(){
        return this.vit;
    }

    public int getAtk(){
        return this.atk;
    }

    public int getDef(){
        return this.def;
    }
}
public class GameRoleStateCaretaker{
    private GameRoleStateMemento memento;

    public void setGameRoleStateMemento(GameRoleStateMemento memento){
        this.memento = memento;
    }  
    public GameRoleStateMemento getGameRoleStateMemento(){
        return this.memento;   
    }  
}
public class App{
        public static void main(String[] args){
                GameRole game = new GameRole();
                game.initState();//初始化状态
                System.out.println("初始化状态:");
                game.displayState();

                GameRoleStateMemento memento = game.saveState();
                GameRoleStateCaretaker caretaker = new GameRoleStateCaretaker();
                caretaker.setGameRoleStateMemento(memento);
               
                game.fight();
                System.out.println("打完Boss状态:");
                game.displayState();

                game.recoveryState(caretaker.getGameRoleStateMemento());
                System.out.println("恢复后状态:");
                game.displayState();

        }
}


 总结:

类似于手机App开发中的墓碑机制。当App退出到后台时,系统要求这样程序其实已经真的退出了,但是用户在切换到该游戏时,希望还能还原之前的状态继续操作,这就需要在该程序到后台前,把当前的状态写入它私有的空间中,当再切换到该App时,可以从这个空间中读取并恢复状态。