一、游戏存进度
class GameRole {
private int vit; // 体力
private int atk; // 攻击力
private int def; // 防御力
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public void StateDisplay() { // 状态显示
System.out.println("角色当前状态:");
System.out.println("体力:" + vit);
System.out.println("攻击力:" + atk);
System.out.println("防御力:" + def);
}
public void GetInitState() { // 获得初始状态
this.vit = 100;
this.atk = 100;
this.def = 100;
}
public void Fight() { // 战斗后数据损耗为0
this.vit = 0;
this.atk = 0;
this.def = 0;
}
}
public class Mementos {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay();
// 保存进度
GameRole backup = new GameRole();
backup.setVit(lixiaoyao.getVit());
backup.setAtk(lixiaoyao.getAtk());
backup.setDef(lixiaoyao.getDef());
// 大战Boss时,损耗严重
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
// 恢复之前状态
lixiaoyao.setVit(backup.getVit());
lixiaoyao.setAtk(backup.getAtk());
lixiaoyao.setDef(backup.getDef());
lixiaoyao.StateDisplay();
}
}
二、备忘录模式
备忘录模式:在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。
class Originator { // 发起人类
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento CreateMemento() { // 创建备忘录
return (new Memento(state));
}
public void SetMemento(Memento memento) { // 恢复备忘录
state = memento.getState();
}
public void Show() { // 显示数据
System.out.println("State=" + state);
}
}
class Memento { // 备忘录类
private String state;
public Memento(String state) {
super();
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
class Caretaker { // 管理者类
private Memento memento;
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
}
public class Mementos {
public static void main(String[] args) {
// TODO Auto-generated method stub
Originator o = new Originator();
o.setState("On"); // 设置o的初始状态为On
o.Show();
// 保存状态
Caretaker c = new Caretaker();
c.setMemento(o.CreateMemento());
o.setState("Off"); // 改变属性
o.Show();
o.SetMemento(c.getMemento()); // 恢复员初始状态
o.Show();
}
}
游戏进度用备忘录模式改进:
class GameRole { // 游戏角色类
private int vit; // 体力
private int atk; // 攻击力
private int def; // 防御力
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
public RoleStateMemento SaveState() { // 保存角色状态
return (new RoleStateMemento(vit, atk, def));
}
public void RecoveryState(RoleStateMemento memento) { // 恢复角色状态
this.vit = memento.getVit();
this.atk = memento.getAtk();
this.def = memento.getDef();
}
public void StateDisplay() { // 状态显示
System.out.println("角色当前状态:");
System.out.println("体力:" + vit);
System.out.println("攻击力:" + atk);
System.out.println("防御力:" + def);
}
public void GetInitState() { // 获得初始状态
this.vit = 100;
this.atk = 100;
this.def = 100;
}
public void Fight() { // 战斗后数据损耗为0
this.vit = 0;
this.atk = 0;
this.def = 0;
}
}
class RoleStateMemento {
private int vit; // 体力
private int atk; // 攻击力
private int def; // 防御力
public RoleStateMemento(int vit, int atk, int def) {
super();
this.vit = vit;
this.atk = atk;
this.def = def;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
class RoleStateCaretaker { // 角色状态管理者类
private RoleStateMemento memento;
public RoleStateMemento getMemento() {
return memento;
}
public void setMemento(RoleStateMemento memento) {
this.memento = memento;
}
}
public class Mementos {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 大战Boss前
GameRole lixiaoyao = new GameRole();
lixiaoyao.GetInitState();
lixiaoyao.StateDisplay();
// 保存进度
RoleStateCaretaker stateAdmin = new RoleStateCaretaker();
stateAdmin.setMemento(lixiaoyao.SaveState());
// 大战Boss时,损耗严重
lixiaoyao.Fight();
lixiaoyao.StateDisplay();
// 恢复之前状态
lixiaoyao.RecoveryState(stateAdmin.getMemento());
lixiaoyao.StateDisplay();
}
}