Java23种设计模式——备忘录模式

该系列文章为博主学习笔记,原文请参考参考链接
本文禁止转载,但是欢迎大家留言区留言交流[微笑]

理(面)解(试)备忘录模式: 当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,用户可以方便地回到一个特定的历史步骤。

public class Game {
    List<RPGGameMemento> list = new ArrayList<RPGGameMemento>();

    public void setGameMemento(RPGGameMemento rpgGameMemento) {
        list.add(rpgGameMemento);
    }

    public RPGGameMemento getGameMemento(int index) {
        return list.get(index);
    }
}
public class MyClass {
    private static Game game = new Game();
    private static int index = -1;

    public static void main(String args[]) {

        RPGGame rpgGame = new RPGGame(100, 100, "第一关");
        play(rpgGame);

        rpgGame.setHP(60);
        rpgGame.setPlace("第二关");
        play(rpgGame);

        rpgGame.setHP(20);
        rpgGame.setPlace("第三关");
        play(rpgGame);

        rpgGame.setHP(0);
        rpgGame.setPlace("第四关");
        play(rpgGame);

        undo(rpgGame);
        undo(rpgGame);

        redo(rpgGame);
    }

    private static void play(RPGGame rpgGame) {
        game.setGameMemento(rpgGame.save());
        index++;

        System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
        if (rpgGame.getHP() == 0) {
            System.out.println("——————游戏结束——————");
        }
    }

    private static void undo(RPGGame rpgGame) {
        System.out.println("——————回退——————");
        index--;
        RPGGameMemento gameMemento = game.getGameMemento(index);
        rpgGame.restore(gameMemento);
        if (rpgGame.getHP() == 0) {
            System.out.println("——————游戏结束——————");
        } else {
            System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
        }
    }

    private static void redo(RPGGame rpgGame){
        System.out.println("——————撤销回退——————");
        index++;
        RPGGameMemento gameMemento = game.getGameMemento(index);
        rpgGame.restore(gameMemento);
        if (rpgGame.getHP() == 0) {
            System.out.println("——————游戏结束——————");
        } else {
            System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
        }
    }
}
public class RPGGame {
    private int HP;
    private int MP;
    private String place;

    public RPGGame(int HP,int MP,String place) {
        this.HP = HP;
        this.MP = MP;
        this.place = place;
    }


    public int getHP() {
        return HP;
    }

    public void setHP(int HP) {
        this.HP = HP;
    }

    public int getMP() {
        return MP;
    }

    public void setMP(int MP) {
        this.MP = MP;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public RPGGameMemento save() {
        return new RPGGameMemento(this.HP,this.MP,this.place);
    }

    public void restore(RPGGameMemento rpgGame) {
        this.HP = rpgGame.getHP();
        this.MP = rpgGame.getMP();
        this.place = rpgGame.getPlace();
    }

}
public class RPGGameMemento {
    private int HP;
    private int MP;
    private String place;

    public RPGGameMemento(int hp, int mp, String place) {
        this.HP=hp;
        this.MP=mp;
        this.place=place;
    }


    public int getHP() {
        return HP;
    }

    public void setHP(int HP) {
        this.HP = HP;
    }

    public int getMP() {
        return MP;
    }

    public void setMP(int MP) {
        this.MP = MP;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值