设计模式 行为型模式 实例 -- 备忘录模式实例:游戏备忘录

01:设计模式 行为型模式 – 备忘录模式(定义、结构、优缺点 适用场景分析)
02:设计模式 行为型模式 – 备忘录模式 具体实例:游戏挑战BOSS
03:设计模式 行为型模式 实例 – 备忘录模式实例

1. 备忘录模式实例


1.1 需求:

玩孤胆枪手游戏时,主角有“武器”,“生命值”,“技能值”,
“防护值”等属性并即时显示,游戏“有当前进度值”,
玩家在游戏时随时可以保存游戏进度及从以前进度回复,
请用备忘录模式模拟这一过程。

1.2 类图分析:

在这里插入图片描述


1.3 代码实现:


1.3.1 GameRole

package ex09.memorandum;

/**
 * ClassName: GameRole
 * Description: 游戏角色类:发起人角色一个的属性
 *
 * @author Tianjiao
 * @date 2021/11/22 17:04
 */
public class GameRole {
    // “武器”
    private String weapon;
    // “生命值”
    private int lifeValue;
    // “技能值”
    private int skillValue;
    // “防护值”
    private int protectionValue;

    public GameRole(String weapon, int lifeValue, int skillValue, int protectionValue) {
        this.weapon = weapon;
        this.lifeValue = lifeValue;
        this.skillValue = skillValue;
        this.protectionValue = protectionValue;
    }

    public String getWeapon() {
        return weapon;
    }

    public void setWeapon(String weapon) {
        this.weapon = weapon;
    }

    public int getLifeValue() {
        return lifeValue;
    }

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

    public int getSkillValue() {
        return skillValue;
    }

    public void setSkillValue(int skillValue) {
        this.skillValue = skillValue;
    }

    public int getProtectionValue() {
        return protectionValue;
    }

    public void setProtectionValue(int protectionValue) {
        this.protectionValue = protectionValue;
    }

    @Override
    public String toString() {
        return "[" +
                "weapon='" + weapon + '\'' +
                ", lifeValue=" + lifeValue +
                ", skillValue=" + skillValue +
                ", protectionValue=" + protectionValue +
                ']';
    }
}

1.3.2 Game:发起人角色

package ex09.memorandum;

/**
 * ClassName: Game
 * Description: 游戏类:发起人角色
 *
 * @author Tianjiao
 * @date 2021/11/22 17:15
 */
public class Game {
    // 游戏进度
    private String gameProgress;
    // 游戏角色
    private GameRole gameRole;

    public Game(String gameProgress, GameRole gameRole) {
        this.gameProgress = gameProgress;
        this.gameRole = gameRole;
    }

    public Game saveState() {
        return this;
    }

    public void recoverState(Game roleStateMemento) {
        this.gameProgress = roleStateMemento.getGameProgress();
        this.gameRole.setWeapon(roleStateMemento.getGameRole().getWeapon());
        this.gameRole.setLifeValue(roleStateMemento.getGameRole().getLifeValue());
        this.gameRole.setProtectionValue(roleStateMemento.getGameRole().getProtectionValue());
        this.gameRole.setSkillValue(roleStateMemento.getGameRole().getSkillValue());
    }

    //恢复角色状态

    public String getGameProgress() {
        return gameProgress;
    }

    public void setGameProgress(String gameProgress) {
        this.gameProgress = gameProgress;
    }

    public GameRole getGameRole() {
        return gameRole;
    }

    public void setGameRole(GameRole gameRole) {
        this.gameRole = gameRole;
    }

    @Override
    public String toString() {
        return "[" +
                "gameProgress='" + gameProgress + '\'' +
                ", gameRole=" + gameRole.toString() +
                ']';
    }
}

1.3.3 GameMemento :游戏备忘录角色

package ex09.memorandum;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: GameMemento
 * Description: 游戏备忘录角色:可以记录游戏的进度和对象的游戏角色状态
 *
 * @author Tianjiao
 * @date 2021/11/22 17:18
 */
public class GameMemento {
    private static final Map<String, GameRole> gameVersionMap = new HashMap();

    public void addVersionState(String gameProgress, GameRole gameRole) {
        GameRole role = new GameRole(gameRole.getWeapon(), gameRole.getLifeValue(), gameRole.getSkillValue(), gameRole.getProtectionValue());
        gameVersionMap.put(gameProgress, role);
    }

    public void deleteVersionState(String gameProgress) {
        gameVersionMap.remove(gameProgress);
    }

    public GameRole getVersionState(String gameProgress) {
        return gameVersionMap.get(gameProgress);
    }

    public void showVersions() {
        if (gameVersionMap.size() != 0) {
            for (Map.Entry<String, GameRole> entry : gameVersionMap.entrySet()) {
                System.out.print("{" + entry.getKey() + " : " + entry.getValue().toString() + "}\n");
            }
        } else {
            System.out.println("对不起, 没有历史版本信息.");
        }
    }
}

1.3.4 GameCaretaker:管理者角色

package ex09.memorandum;

/**
 * ClassName: GameCaretaker
 * Description: 管理者角色:对备忘录进行管理, 提供保存于获取备忘录的功能
 *
 * @author Tianjiao
 * @date 2021/11/22 17:19
 */
public class GameCaretaker {
    private final GameMemento gameMemento;

    public GameCaretaker(GameMemento gameMemento) {
        this.gameMemento = gameMemento;
    }

    public void saveGameState(Game game) {
        this.gameMemento.addVersionState(game.getGameProgress(), game.getGameRole());
    }

    public Game getGameState(String gameProgress) {
        return new Game(gameProgress, this.gameMemento.getVersionState(gameProgress));
    }

    public void deleteGameState(String gameProgress) {
        this.gameMemento.deleteVersionState(gameProgress);
    }

    public void showVersions() {
        this.gameMemento.showVersions();
    }
}

1.3.5 Client:客户端类

package ex09.memorandum;

/**
 * ClassName: Client
 * Description: 客户端类:测试类
 *
 * @author Tianjiao
 * @date 2021/11/22 19:51
 */
public class Client {
    public static void main(String[] args) {
        // 创建游戏对象
        GameRole gameRole = new GameRole("废铁刀", 100, 100, 100);
        Game game = new Game("0.00%", gameRole);
        // 创建管理者对象
        GameCaretaker gameCaretaker = new GameCaretaker(new GameMemento());
        // 1. 存储初始状态
        gameCaretaker.saveGameState(game);

        // 2. 第一次改变游戏状态
        game.setGameProgress("27%");
        gameRole.setWeapon("白银刀");
        gameRole.setLifeValue(90);
        gameRole.setSkillValue(90);
        gameRole.setProtectionValue(90);

        // 3. 存储第一次改变的状态
        gameCaretaker.saveGameState(game);

        // 4. 第二次改变游戏状态
        game.setGameProgress("50%");
        gameRole.setWeapon("黄金刀");
        gameRole.setLifeValue(68);
        gameRole.setSkillValue(68);
        gameRole.setProtectionValue(68);

        // 5. 存储第二次改变的状态
        gameCaretaker.saveGameState(game);

        // 6. 第三次改变状态
        game.setGameProgress("63%");
        gameRole.setWeapon("铂金刀");
        gameRole.setLifeValue(56);
        gameRole.setSkillValue(56);
        gameRole.setProtectionValue(56);

        // 7. 存储第三次改变游戏的状态
        gameCaretaker.saveGameState(game);

        // 打印当前的游戏状态
        System.out.println("当前的游戏状态为:");
        System.out.println(game.toString() + "\n");

        // 打印游戏版本信息
        System.out.println("历代的游戏版本信息为:");
        gameCaretaker.showVersions();

        // 回到游戏进度为27%的版本
        game.recoverState(gameCaretaker.getGameState("27%"));

        // 打印当前的游戏状态
        System.out.println("\n当前的游戏状态为:");
        System.out.println(game.toString() + "\n");

        // 回到游戏进度为63%的版本
        game.recoverState(gameCaretaker.getGameState("63%"));
        // 打印当前的游戏状态
        System.out.println("当前的游戏状态为:");
        System.out.println(game.toString() + "\n");
    }
}

1.3.6 运行结果:

在这里插入图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeJiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值