package com.wangbiao.design.memento; /** * * @Title: GameRole.java * @Package com.wangbiao.design.memento * @Description: TODO * @author wangbiao * @date 2014-9-22 上午11:52:44 * @version V1.0 */ public class GameRole { private String name; private int attackNum; private int bloodNum; public GameRole() { // TODO Auto-generated constructor stub } public GameRole(String name,int attackNum,int bloodNum) { this.name = name; this.attackNum = attackNum; this.bloodNum = bloodNum; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAttackNum() { return attackNum; } public void setAttackNum(int attackNum) { this.attackNum = attackNum; } public int getBloodNum() { return bloodNum; } public void setBloodNum(int bloodNum) { this.bloodNum = bloodNum; } public GameRole initRole(){ return new GameRole("Enchanter", 100, 100); } //save data into Memento public Memento saveRoleInfo(){ return new Memento(this.attackNum,this.bloodNum); } //get the old role info public boolean getBeforeRoleInfo(Memento memento){ if(memento != null){ this.attackNum = memento.getAttackNum(); this.bloodNum = memento.getBloodNum(); return true; } return false; } public void fightWithBoss(){ System.out.println("figth with boss"); this.attackNum = this.attackNum/5; this.bloodNum = this.bloodNum/5; } public void displayRoleInfo(){ System.out.println("攻击指数:" +this.attackNum +" ----"+ "血指数:"+this.bloodNum); } } package com.wangbiao.design.memento; /** * * @Title: Memento.java * @Package com.wangbiao.design.memento * @Description: TODO * @author wangbiao * @date 2014-9-22 下午12:11:12 * @version V1.0 */ public class Memento { private int attackNum; private int bloodNum; public Memento() { // TODO Auto-generated constructor stub } public Memento(int attackNum,int bloodNum) { this.attackNum = attackNum; this.bloodNum = bloodNum; } public int getAttackNum() { return attackNum; } public void setAttackNum(int attackNum) { this.attackNum = attackNum; } public int getBloodNum() { return bloodNum; } public void setBloodNum(int bloodNum) { this.bloodNum = bloodNum; } } package com.wangbiao.design.memento; /** * * @Title: RoleCaretaker.java * @Package com.wangbiao.design.memento * @Description: TODO * @author wangbiao * @date 2014-9-22 下午12:11:18 * @version V1.0 */ public class RoleCaretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } } /** * @Title: Client.java * @Package com.wangbiao.design.memento * @Description: TODO * @author wangbiao * @date 2014-9-22 下午12:11:03 * @version V1.0 */ package com.wangbiao.design.memento; /** * @Title: Client.java * @Package com.wangbiao.design.memento * @Description: TODO * @author wangbiao * @date 2014-9-22 下午12:11:03 * @version V1.0 */ public class Client { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub GameRole gameRole = new GameRole(); gameRole = gameRole.initRole(); gameRole.displayRoleInfo(); // store GameRole info to RoleCaretaker RoleCaretaker caretaker = new RoleCaretaker(); caretaker.setMemento(gameRole.saveRoleInfo()); //fight with boss gameRole.fightWithBoss(); gameRole.displayRoleInfo(); // get the old role info gameRole.getBeforeRoleInfo(caretaker.getMemento()); gameRole.displayRoleInfo(); // result /* 攻击指数:100 ----血指数:100 figth with boss 攻击指数:20 ----血指数:20 攻击指数:100 ----血指数:100 */ } }
时间: 2024-10-28 11:37:51