设计模式:备忘录模式示例

文章目录

示例一

让我们设计一个更复杂的场景:一个角色状态管理系统,用于游戏中角色的状态保存和恢复。在游戏中,角色可能会经历战斗,其生命值(HP)、魔法值(MP)和金币(Gold)等属性会发生变化。玩家可以在任何时刻保存当前的状态,并在需要时恢复到之前保存的状态。

定义备忘录类(Memento)

java 复制代码
public class CharacterMemento {
    private int hp;
    private int mp;
    private int gold;

    public CharacterMemento(int hp, int mp, int gold) {
        this.hp = hp;
        this.mp = mp;
        this.gold = gold;
    }

    public int getHp() {
        return hp;
    }

    public int getMp() {
        return mp;
    }

    public int getGold() {
        return gold;
    }
}

定义发起人类(Originator)

java 复制代码
public class GameCharacter {
    private int hp;
    private int mp;
    private int gold;

    public GameCharacter(int hp, int mp, int gold) {
        this.hp = hp;
        this.mp = mp;
        this.gold = gold;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public void setMp(int mp) {
        this.mp = mp;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    public int getHp() {
        return hp;
    }

    public int getMp() {
        return mp;
    }

    public int getGold() {
        return gold;
    }

    public CharacterMemento saveState() {
        return new CharacterMemento(hp, mp, gold);
    }

    public void restoreState(CharacterMemento memento) {
        this.hp = memento.getHp();
        this.mp = memento.getMp();
        this.gold = memento.getGold();
    }
}

定义看护人类(Caretaker)

java 复制代码
import java.util.Stack;

public class Caretaker {
    private Stack<CharacterMemento> history = new Stack<>();

    public void saveState(GameCharacter character) {
        history.push(character.saveState());
    }

    public void restoreState(GameCharacter character) {
        if (!history.isEmpty()) {
            character.restoreState(history.pop());
        }
    }
}

使用备忘录模式

java 复制代码
public class Main {
    public static void main(String[] args) {
        GameCharacter character = new GameCharacter(100, 50, 10);
        Caretaker caretaker = new Caretaker();

        // 保存初始状态
        caretaker.saveState(character);

        // 状态变化
        character.setHp(80);
        character.setMp(30);
        character.setGold(20);
        System.out.println("After battle: HP=" + character.getHp() + ", MP=" + character.getMp() + ", Gold=" + character.getGold());

        // 恢复到初始状态
        caretaker.restoreState(character);
        System.out.println("After restore: HP=" + character.getHp() + ", MP=" + character.getMp() + ", Gold=" + character.getGold());
    }
}

输出:

After battle: HP=80, MP=30, Gold=20
After restore: HP=100, MP=50, Gold=10

在这个例子中,GameCharacter类是发起人,它能够保存自己的状态到备忘录对象CharacterMemento中,也能从备忘录对象恢复状态。Caretaker类负责维护这些备忘录对象的历史记录,但不对这些对象进行操作或检查。这个示例展示了备忘录模式在游戏角色状态管理中的应用,帮助实现了角色状态的保存和恢复功能。

示例二

定义备忘录类(Memento)

java 复制代码
public class GameMemento {
    private final int lifePoints;
    private final String location;
    private final int gold;

    public GameMemento(int lifePoints, String location, int gold) {
        this.lifePoints = lifePoints;
        this.location = location;
        this.gold = gold;
    }

    public int getLifePoints() {
        return lifePoints;
    }

    public String getLocation() {
        return location;
    }

    public int getGold() {
        return gold;
    }
}

定义发起人类(Originator)

java 复制代码
public class GameCharacter {
    private int lifePoints;
    private String location;
    private int gold;

    public void setLifePoints(int lifePoints) {
        this.lifePoints = lifePoints;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    // 保存状态到备忘录
    public GameMemento save() {
        return new GameMemento(lifePoints, location, gold);
    }

    // 从备忘录恢复状态
    public void restore(GameMemento memento) {
        this.lifePoints = memento.getLifePoints();
        this.location = memento.getLocation();
        this.gold = memento.getGold();
    }
}

定义看护人类(Caretaker)

java 复制代码
import java.util.Stack;

public class GameSaver {
    private Stack<GameMemento> mementos = new Stack<>();

    public void saveGame(GameCharacter character) {
        mementos.push(character.save());
    }

    public void loadGame(GameCharacter character) {
        if (!mementos.isEmpty()) {
            character.restore(mementos.pop());
        }
    }
}

使用备忘录模式

java 复制代码
public class Main {
    public static void main(String[] args) {
        GameCharacter character = new GameCharacter();
        GameSaver saver = new GameSaver();

        // 初始状态
        character.setLifePoints(100);
        character.setLocation("Village");
        character.setGold(50);
        System.out.println("Initial state");

        // 游戏进行,状态改变
        character.setLifePoints(80);
        character.setLocation("Dungeon");
        character.setGold(100);
        System.out.println("Game progressed, state changed");

        // 保存游戏
        saver.saveGame(character);

        // 继续游戏,状态再次改变
        character.setLifePoints(30);
        character.setLocation("Deep Dungeon");
        character.setGold(150);
        System.out.println("Game progressed further, state changed again");

        // 加载游戏,恢复到之前的状态
        saver.loadGame(character);
        System.out.println("Game loaded, state restored");
    }
}

在这个例子中,GameCharacter类代表游戏中的角色,它可以保存自己的状态到GameMemento备忘录中,并能从备忘录中恢复状态。GameSaver类充当看护人的角色,负责管理备忘录,实现游戏的存档和读档功能。

这个简化的游戏存档例子展示了备忘录模式在实际应用中的应用方式,通过这种方式,我们可以在不暴露GameCharacter内部状态的前提下,实现游戏状态的保存和恢复。

相关推荐
神仙别闹24 分钟前
基于java的改良版超级玛丽小游戏
java
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭1 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
暮湫1 小时前
泛型(2)
java
超爱吃士力架1 小时前
邀请逻辑
java·linux·后端
南宫生1 小时前
力扣-图论-17【算法学习day.67】
java·学习·算法·leetcode·图论
转码的小石1 小时前
12/21java基础
java
李小白661 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp2 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
装不满的克莱因瓶2 小时前
【Redis经典面试题六】Redis的持久化机制是怎样的?
java·数据库·redis·持久化·aof·rdb
n北斗2 小时前
常用类晨考day15
java