/**
- @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;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。