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