第18章《备忘录模式》

前端之家收集整理的这篇文章主要介绍了第18章《备忘录模式》前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

功能:

功能。命令模式可以使用备忘录来存储可撤销操作的状态。

功能比较复杂的,需要维护或记录属性历史的类 or 需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。

403b775a33a46fc9d78eb96008c.jpg" alt="">

属性:生命力、攻击力、防御力。在角色a攻击角色b之前,这三个属性是一个状态值;攻击完b后,这三个属性值是另一个状态值;此时借助角色存储备忘录Memento,恢复a攻击前的状态值。

代码结构图,然后给出代码

/**

  • @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;
    }
    }

猜你在找的设计模式相关文章