zoukankan      html  css  js  c++  java
  • 第三节:备忘录模式——游戏角色恢复状态实例

    一、需求说明

      1、应用实例要求

        游戏角色有攻击力和防御力,在大战 Boss 前保存自身的状态(攻击力和防御力),当大战 Boss 后攻击力和防御力下降,从备忘录对象恢复到大战前的状态。

     

    二、思路分析和图解

      

    三、代码实现

       备忘录对象:

     1 public class Memento {
     2 
     3     //攻击力
     4     private int vit;
     5     //防御力
     6     private int def;
     7 
     8     public Memento(int vit, int def) {
     9         this.vit = vit;
    10         this.def = def;
    11     }
    12 
    13     public int getVit() {
    14         return vit;
    15     }
    16 
    17     public void setVit(int vit) {
    18         this.vit = vit;
    19     }
    20 
    21     public int getDef() {
    22         return def;
    23     }
    24 
    25     public void setDef(int def) {
    26         this.def = def;
    27     }
    28 }

      游戏角色:

     1 public class GameRole {
     2 
     3     private int vit;
     4     private int def;
     5 
     6     public int getVit() {
     7         return vit;
     8     }
     9 
    10     public void setVit(int vit) {
    11         this.vit = vit;
    12     }
    13 
    14     public int getDef() {
    15         return def;
    16     }
    17 
    18     public void setDef(int def) {
    19         this.def = def;
    20     }
    21 
    22     /**
    23      * 创建Memento,即根据当前的状态得到 Memento
    24      * @return
    25      */
    26     public Memento createMemento() {
    27         return new Memento(vit, def);
    28     }
    29 
    30     /**
    31      * 从备忘录对象,恢复 gameRole 的状态
    32      * @param memento
    33      */
    34     public void recoverGameRoleFromMemento(Memento memento) {
    35         this.vit = memento.getVit();
    36         this.def = memento.getDef();
    37     }
    38 
    39 
    40     /**
    41      *  显示当前游戏角色的状态
    42      */
    43     public void display() {
    44         System.out.println("游戏角色的攻击力:" + this.vit + "防御力:" + this.def);
    45     }
    46 }

      Caretaker 守护对象:

     1 /**
     2  * 守护对象,用于保存游戏角色状态
     3  */
     4 public class Caretaker {
     5 
     6     //如果只保存一次状态
     7     private Memento memento;
     8     //对 GameRole 保存多次状态
     9     //private ArrayList<Memento> mementos;
    10     //对多个游戏角色保存多个状态
    11     //private HashMap<String, List<Memento>> rolesMementos;
    12 
    13 
    14     public Memento getMemento() {
    15         return memento;
    16     }
    17 
    18     public void setMemento(Memento memento) {
    19         this.memento = memento;
    20     }
    21 }

      客户端:

     1 public class Client {
     2     public static void main(String[] args) {
     3 
     4         GameRole gameRole = new GameRole();
     5         gameRole.setVit(100);
     6         gameRole.setDef(100);
     7 
     8         System.out.println("和Boss大战前的状态");
     9         gameRole.display();
    10 
    11         //把当前状态保存到 Caretaker
    12         Caretaker caretaker = new Caretaker();
    13         caretaker.setMemento(gameRole.createMemento());
    14 
    15         System.out.println("和Boss大战....");
    16 
    17         gameRole.setDef(50);
    18         gameRole.setVit(60);
    19 
    20         System.out.println("和Boss大战后的状态");
    21         gameRole.display();
    22 
    23         System.out.println("大战后,使用备忘录对象恢复到对战前");
    24         gameRole.recoverGameRoleFromMemento(caretaker.getMemento());
    25 
    26         System.out.println("恢复后的状态");
    27         gameRole.display();
    28 
    29     }
    30 }

     

  • 相关阅读:
    多线程之 Final变量 详解
    多线程之 Volatile 变量 详解
    并发安全问题之HashMap
    探索设计模式目录
    MYsql 锁详解 锁 与索引的关系
    JVM GC 相关
    sql 注入 及 in 注入
    00
    03
    02
  • 原文地址:https://www.cnblogs.com/niujifei/p/14454489.html
Copyright © 2011-2022 走看看