您的位置:首页 > 编程语言 > Java开发

Java23种设计模式——备忘录模式

2017-11-23 14:24 423 查看
该系列文章为博主学习笔记,原文请参考参考链接

本文禁止转载,但是欢迎大家留言区留言交流[微笑]

理(面)解(试)备忘录模式: 当新的状态无效或者存在问题时,可以使用暂时存储起来的备忘录将状态复原,用户可以方便地回到一个特定的历史步骤。

public class Game {
List<RPGGameMemento> list = new ArrayList<RPGGameMemento>();

public void setGameMemento(RPGGameMemento rpgGameMemento) {
list.add(rpgGameMemento);
}

public RPGGameMemento getGameMemento(int index) {
return list.get(index);
}
}


public class MyClass {
private static Game game = new Game();
private static int index = -1;

public static void main(String args[]) {

RPGGame rpgGame = new RPGGame(100, 100, "第一关");
play(rpgGame);

rpgGame.setHP(60);
rpgGame.setPlace("第二关");
play(rpgGame);

rpgGame.setHP(20);
rpgGame.setPlace("第三关");
play(rpgGame);

rpgGame.setHP(0);
rpgGame.setPlace("第四关");
play(rpgGame);

undo(rpgGame);
undo(rpgGame);

redo(rpgGame);
}

private static void play(RPGGame rpgGame) {
game.setGameMemento(rpgGame.save());
index++;

System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
if (rpgGame.getHP() == 0) {
System.out.println("——————游戏结束——————");
}
}

private static void undo(RPGGame rpgGame) {
System.out.println("——————回退——————");
index--;
RPGGameMemento gameMemento = game.getGameMemento(index);
rpgGame.restore(gameMemento);
if (rpgGame.getHP() == 0) {
System.out.println("——————游戏结束——————");
} else {
System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
}
}

private static void redo(RPGGame rpgGame){
System.out.println("——————撤销回退——————");
index++;
RPGGameMemento gameMemento = game.getGameMemento(index);
rpgGame.restore(gameMemento);
if (rpgGame.getHP() == 0) {
System.out.println("——————游戏结束——————");
} else {
System.out.println(rpgGame.getPlace() + "  HP=" + rpgGame.getHP() + "  MP=" + rpgGame.getMP());
}
}
}


public class RPGGame {
private int HP;
private int MP;
private String place;

public RPGGame(int HP,int MP,String place) {
this.HP = HP;
this.MP = MP;
this.place = place;
}

public int getHP() {
return HP;
}

public void setHP(int HP) {
this.HP = HP;
}

public int getMP() {
return MP;
}

public void setMP(int MP) {
this.MP = MP;
}

public String getPlace() {
return place;
}

public void setPlace(String place) {
this.place = place;
}

public RPGGameMemento save() {
return new RPGGameMemento(this.HP,this.MP,this.place);
}

public void restore(RPGGameMemento rpgGame) {
this.HP = rpgGame.getHP();
this.MP = rpgGame.getMP();
this.place = rpgGame.getPlace();
}

}


public class RPGGameMemento {
private int HP;
private int MP;
private String place;

public RPGGameMemento(int hp, int mp, String place) {
this.HP=hp;
this.MP=mp;
this.place=place;
}

public int getHP() {
return HP;
}

public void setHP(int HP) {
this.HP = HP;
}

public int getMP() {
return MP;
}

public void setMP(int MP) {
this.MP = MP;
}

public String getPlace() {
return place;
}

public void setPlace(String place) {
this.place = place;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: