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

文章目录

示例一

让我们设计一个更复杂的场景:一个角色状态管理系统,用于游戏中角色的状态保存和恢复。在游戏中,角色可能会经历战斗,其生命值(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内部状态的前提下,实现游戏状态的保存和恢复。

相关推荐
是梦终空17 分钟前
JAVA毕业设计210—基于Java+Springboot+vue3的中国历史文化街区管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·历史文化街区管理·景区管理
基哥的奋斗历程41 分钟前
学到一些小知识关于Maven 与 logback 与 jpa 日志
java·数据库·maven
m0_5127446441 分钟前
springboot使用logback自定义日志
java·spring boot·logback
十二同学啊1 小时前
JSqlParser:Java SQL 解析利器
java·开发语言·sql
老马啸西风1 小时前
Plotly 函数图像绘制
java
方圆想当图灵1 小时前
缓存之美:万文详解 Caffeine 实现原理(上)
java·缓存
gyeolhada1 小时前
计算机组成原理(计算机系统3)--实验八:处理器结构拓展实验
java·前端·数据库·嵌入式硬件
Java&Develop1 小时前
jeecg后端登录接口
java
蒙双眼看世界1 小时前
IDEA运行Java项目总会报程序包xxx不存在
java·spring·maven
graceyun3 小时前
C语言进阶习题【1】指针和数组(4)——指针笔试题3
android·java·c语言