首页 > 代码库 > java设计模式_备忘录模式
java设计模式_备忘录模式
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 */ } }
java设计模式_备忘录模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。