游戏角色状态恢复问题
传统方案解决游戏角色恢复
传统的方式的问题分析
备忘录模式基本介绍
基本介绍
原理类图
代码实现
public class Client {
public static void main(String[] args) {
Originator originator = new Originator();
Caretaker caretaker = new Caretaker();
originator.setState("状态1");
//保存当前状态
caretaker.add(originator.saveStateMemento());
originator.setState("状态2");
caretaker.add(originator.saveStateMemento());
originator.setState("状态3");
caretaker.add(originator.saveStateMemento());
System.out.println("当前状态::"+originator.getState());
//得到状态1,将originater恢复状态1
originator.getStateFromMemento(caretaker.get(0));
System.out.println("恢复到状态1:"+originator.getState());
}
}
public class Caretaker {
/**
* 会有很多的备忘录对象
*/
private List<Memento> mementos = new ArrayList<>();
public void add(Memento memento){
mementos.add(memento);
}
/**
* 获取到第index哥Originator的备忘录对象(即保存状态)
* @param index
* @return
*/
public Memento get(Integer index){
return mementos.get(index);
}
}
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}
public class Originator {
/**
* 状态
*/
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
/**
* 编写方法,保存一个状态对象memnto
* 返回Memnto
*/
public Memento saveStateMemento(){
return new Memento(state);
}
/**
* 通过备忘录对象,恢复状态
*/
public void getStateFromMemento(Memento memento){
state = memento.getState();
}
}
角色状态恢复实例
思路分析和图解
代码实现
public class Client {
public static void main(String[] args) {
//创建游戏结束
GameRole gameRole = new GameRole();
gameRole.setVit(100);
gameRole.setDef(100);
System.out.println("和boss干架前的状态");
gameRole.display();
//把当前状态保存到Caretaker
Caretaker caretaker = new Caretaker();
caretaker.setMemento(gameRole.createMemento());
System.out.println("和boss大战");
gameRole.setDef(70);
gameRole.setVit(70);
gameRole.display();
System.out.println("大战后 使用备忘录对象恢复");
gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
System.out.println("恢复后的状态");
gameRole.display();
}
}
/**
* @author: LiDeLin 18234780819@163.com
* @date: 2019/11/24
* @description : 守护者对象,保存游戏角色的状态
*/
public class Caretaker {
/**
* 如果只保存一次状态
*/
private Memento memento;
/**
* 如果对GameRole保存多次状态
*
*/
private List<Memento> mementoList = new ArrayList<>();
/**
* 对多个游戏角色保存多个状态
*
*/
private Map<String,Memento> rolesMenemtos = new HashMap<>();
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
this.memento = memento;
}
public List<Memento> getMementoList() {
return mementoList;
}
public void setMementoList(List<Memento> mementoList) {
this.mementoList = mementoList;
}
public Map<String, Memento> getRolesMenemtos() {
return rolesMenemtos;
}
public void setRolesMenemtos(Map<String, Memento> rolesMenemtos) {
this.rolesMenemtos = rolesMenemtos;
}
}
public class GameRole {
private Integer vit;
private Integer def;
/**
* 创建Memento,即根据当前状态得到Memento
*/
public Memento createMemento(){
return new Memento(vit,def);
}
/**
* 从备忘录对象恢复gameRole的状态
*
*/
public void recoverGameRoleFromMemento(Memento memento){
this.vit = memento.getVit();
this.def = memento.getDef();
}
/**
* 显示当前状态
*/
public void display(){
System.out.println("当前攻击力"+this.vit+"当前防御力"+this.def);
}
public Integer getVit() {
return vit;
}
public void setVit(Integer vit) {
this.vit = vit;
}
public Integer getDef() {
return def;
}
public void setDef(Integer def) {
this.def = def;
}
}
public class Memento {
/**
* 攻击力
*/
private int vit;
/**
* 防御力
*/
private int def;
public Memento(int vit, int def) {
this.vit = vit;
this.def = def;
}
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;
}
}
备忘录模式的注意事项和细节