美文网首页
备忘录模式

备忘录模式

作者: 要学的东西太多了 | 来源:发表于2018-11-04 16:21 被阅读0次

备忘录对象是一个用来存储另外一个对象内部状态的快照的对象。备忘录模式的用意是在不破坏封装的条件下,将一个对象的状态捕捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态。
示例如下:

白箱模式,备忘录角色内部存储对象对所有对象都公开。

public class Mement {
    public static void main(String[] a){
        Mement mement=new Mement();
        Game game=mement.new Game("张三",100,100,100);
        Caretaker caretaker=mement.new Caretaker();
        game.play();
        game.play();
        caretaker.setGameMement(game.createGameMement());
        game.play();
        game.play();
        game.reloadGame(caretaker.getGameMement());
    }

    private static void soutGameStatus(Game game){
        System.out.println(game.getRoleName()+"生命值:"+game.getLife());
        System.out.println(game.getRoleName()+"魔法值:"+game.getMagical());
        System.out.println(game.getRoleName()+"防御值:"+game.getDefense());
        System.out.println();
    }

    class Game{
        private String roleName;
        private int life;
        private int magical;
        private int defense;

        public Game(String roleName, int life, int magical, int defense) {
            this.roleName = roleName;
            this.life = life;
            this.magical = magical;
            this.defense = defense;
            soutGameStatus(this);
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }

        public int getLife() {
            return life;
        }

        public void setLife(int life) {
            this.life = life;
        }

        public int getMagical() {
            return magical;
        }

        public void setMagical(int magical) {
            this.magical = magical;
        }

        public int getDefense() {
            return defense;
        }

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

        public void play(){
            life-=10;
            magical-=8;
            defense-=6;
            soutGameStatus(this);
        }

        public GameMement createGameMement(){
            return new GameMement(roleName,life,magical,defense);
        }

       public void reloadGame(GameMement gameMement){
            System.out.println("\n读档\n");
            roleName = gameMement.getRoleName();
            life = gameMement.getLife();
            magical = gameMement.getMagical();
            defense = gameMement.getDefense();
            soutGameStatus(this);
        }
    }

    class GameMement{
        private String roleName;
        private int life;
        private int magical;
        private int defense;

        public GameMement(String roleName, int life, int magical, int defense) {
            this.roleName = roleName;
            this.life = life;
            this.magical = magical;
            this.defense = defense;
        }

        public String getRoleName() {
            return roleName;
        }

        public int getLife() {
            return life;
        }

        public int getMagical() {
            return magical;
        }

        public int getDefense() {
            return defense;
        }
    }

    class Caretaker{
        private GameMement gameMement;

        public GameMement getGameMement() {
            return gameMement;
        }

        public void setGameMement(GameMement gameMement) {
            System.out.println("\n存档\n");
            this.gameMement = gameMement;
        }
    }
}

黑箱模式,备忘录对象内部存储对象只对发起者公开,其他对象无法访问。

public class Mement {
    public static void main(String[] a){
        Mement mement=new Mement();
        Game game=mement.new Game("张三",100,100,100);
        Caretaker caretaker=mement.new Caretaker();
        game.play();
        game.play();
        caretaker.saveIMement(game.createGameMement());
        game.play();
        game.play();
        game.reloadGame(caretaker.getGameMement());
    }

    private static void soutGameStatus(Game game){
        System.out.println(game.getRoleName()+"生命值:"+game.getLife());
        System.out.println(game.getRoleName()+"魔法值:"+game.getMagical());
        System.out.println(game.getRoleName()+"防御值:"+game.getDefense());
        System.out.println();
    }

    class Game{
        private String roleName;
        private int life;
        private int magical;
        private int defense;

        public Game(String roleName, int life, int magical, int defense) {
            this.roleName = roleName;
            this.life = life;
            this.magical = magical;
            this.defense = defense;
            soutGameStatus(this);
        }

        public String getRoleName() {
            return roleName;
        }

        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }

        public int getLife() {
            return life;
        }

        public void setLife(int life) {
            this.life = life;
        }

        public int getMagical() {
            return magical;
        }

        public void setMagical(int magical) {
            this.magical = magical;
        }

        public int getDefense() {
            return defense;
        }

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

        public void play(){
            life-=10;
            magical-=8;
            defense-=6;
            soutGameStatus(this);
        }

        public IMement createGameMement(){
            return new GameMement(roleName,life,magical,defense);
        }

        public void reloadGame(IMement gameMement){
            System.out.println("\n读档\n");
            roleName = ((GameMement)gameMement).getRoleName();
            life = ((GameMement)gameMement).getLife();
            magical = ((GameMement)gameMement).getMagical();
            defense = ((GameMement)gameMement).getDefense();
            soutGameStatus(this);
        }

        private class GameMement implements IMement{
            private String roleName;
            private int life;
            private int magical;
            private int defense;

            private GameMement(String roleName, int life, int magical, int defense) {
                this.roleName = roleName;
                this.life = life;
                this.magical = magical;
                this.defense = defense;
            }

            private String getRoleName() {
                return roleName;
            }

            private int getLife() {
                return life;
            }

            private int getMagical() {
                return magical;
            }

            private int getDefense() {
                return defense;
            }
        }
    }

    interface IMement{

    }

    class Caretaker{
        private IMement gameMement;

        public IMement getGameMement() {
            return gameMement;
        }

        public void saveIMement(IMement gameMement) {
            System.out.println("\n存档\n");
            this.gameMement = gameMement;
        }
    }
}

相关文章

网友评论

      本文标题:备忘录模式

      本文链接:https://www.haomeiwen.com/subject/japrxqtx.html