大话设计模式 备忘录模式

来源:互联网 发布:js日期格式化函数 编辑:程序博客网 时间:2024/06/05 21:54

游戏保存

package org.ustc.memo;public class GameRole {private int vatality;private int defence;private int offence;public GameRole() {vatality = 100;defence = 100; offence = 100;}public void fight(){vatality = 0;defence = 0;offence = 0 ;}public void display(){System.out.println("vatality:"+vatality+"defence"+defence+"offence"+offence);}public void recoveryFromMemo(Memo memo){this.defence = memo.getDefence();this.offence = memo.getOffence();this.vatality = memo.getVatality();}public Memo saveToMemo(){return new Memo(this.vatality,this.offence,this.defence);}}

package org.ustc.memo;public class Memo {private int vatality;private int offence;private int defence;public int getVatality() {return vatality;}public void setVatality(int vatality) {this.vatality = vatality;}public int getOffence() {return offence;}public void setOffence(int offence) {this.offence = offence;}public int getDefence() {return defence;}public void setDefence(int defence) {this.defence = defence;}public Memo(int vatality, int offence, int defence) {super();this.vatality = vatality;this.offence = offence;this.defence = defence;}}

package org.ustc.memo;public class RoleStateTaker {private Memo memo;public Memo getMemo() {return memo;}public void setMemo(Memo memo) {this.memo = memo;}}
package org.ustc.memo;public class Main {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubGameRole role = new GameRole();role.display();RoleStateTaker roleState = new RoleStateTaker();roleState.setMemo(role.saveToMemo());role.fight();role.display();role.recoveryFromMemo(roleState.getMemo());role.display();}}

代码结构图:



原创粉丝点击