


/**
- @Author: cxh
- @CreateTime: 18/1/16 09:26
- @ProjectName: JavaBaseTest
*/
public class CareTaker{
private RoleStateMemento memento;
CareTaker(RoleStateMemento memento){
this.memento=memento;
}
//get
public RoleStateMemento getMemento() {
return memento;
}
}
/**
- @Author: cxh
- @CreateTime: 18/1/16 09:38
- @ProjectName: JavaBaseTest
*/
public class Client {
public static void main(String[] args) {
GameRole role=new GameRole(11,22,33);
//攻击前状态
role.beforeAttack();
//保存状态
RoleStateMemento memento=role.getMemento();
CareTaker careTaker=new CareTaker(memento);
//攻击后状态
role.afterAttack();
//恢复后状态
role.recover(careTaker);
}
}
/**
-
@Author: cxh
-
@CreateTime: 18/1/16 09:12
-
@ProjectName: JavaBaseTest
*/
public class GameRole {
private int lifeLenth;
private int attackLength;
private int defendLength;
GameRole(int life,int attack,int defend){
this.lifeLenth=life;
this.attackLength=attack;
this.defendLength=defend;
}
//保存状态
public RoleStateMemento getMemento(){
return new RoleStateMemento(lifeLenth,attackLength,defendLength);
}
//攻击前状态
public void beforeAttack(){
printState("攻击前");
}
//攻击后状态
public void afterAttack(){
this.lifeLenth=0;
this.attackLength=0;
this.defendLength=0;
printState("攻击后");
}
//恢复状态
public void recover(CareTaker careTaker){
this.lifeLenth=careTaker.getMemento().getLifeLen();
this.attackLength=careTaker.getMemento().getAttackLen();
this.defendLength=careTaker.getMemento().getDefendLen();
printState("恢复后");
}
//输出当前状态
private void printState(String time){
System.out.println(time+"状态: 生命力"+lifeLenth+",攻击力"+attackLength+",防守力"+defendLength);
}
}
/**
-
@Author: cxh
-
@CreateTime: 18/1/16 09:15
-
@ProjectName: JavaBaseTest
*/
public class RoleStateMemento{
private int lifeLen;
private int attackLen;
private int defendLen;
RoleStateMemento(int life,int defend){
this.lifeLen=life;
this.attackLen=attack;
this.defendLen=defend;
}
//get
public int getLifeLen() {
return lifeLen;
}
public int getAttackLen() {
return attackLen;
}
public int getDefendLen() {
return defendLen;
}
}
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|