溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java使用備忘錄模式實現(xiàn)過關類游戲功能詳解

發(fā)布時間:2020-10-07 09:49:51 來源:腳本之家 閱讀:224 作者:chengqiuming 欄目:編程語言

本文實例講述了Java使用備忘錄模式實現(xiàn)過關類游戲功能。分享給大家供大家參考,具體如下:

一.模式定義

備忘錄模式,在不破壞封閉的前提下,捕獲一個對象的內(nèi)部狀態(tài),并在該對象外部保存這個狀態(tài)。這樣以后就可將該對象恢復到原先保存的狀態(tài)。

二.模式舉例

1模式分析

我們借用過關類游戲來說明這一模式。

Java使用備忘錄模式實現(xiàn)過關類游戲功能詳解

2備忘錄模式靜態(tài)類圖

Java使用備忘錄模式實現(xiàn)過關類游戲功能詳解

3代碼示例(黑箱備忘錄模式)

3.1創(chuàng)建備忘錄窄接口一INarrowMemento

package com.demo.memento;
/**
 * 備忘錄窄接口(不提供任何方法,外部對象不能訪問備忘錄對象內(nèi)部信息)
 *
 * @author
 *
 */
public interface INarrowMemento {
}

3.2備忘錄發(fā)起者一Hero

package com.demo.originator;
import java.util.Random;
import com.demo.memento.INarrowMemento;
/**
 * 挑戰(zhàn)者
 *
 * @author
 *
 */
public class Hero {
  // 血液值
  private int blood;
  // 武力值
  private int sword;
  // 隨機數(shù)
  private final Random random = new Random();
  // 構造方法初始化 內(nèi)容
  public Hero() {
    this.blood = 100;
    this.sword = 100;
  }
  // 創(chuàng)建備忘錄保存內(nèi)容
  public INarrowMemento createMemento() {
    System.out.println("創(chuàng)建備忘錄...");
    return new Memento(this.blood, this.sword);
  }
  // 恢復備忘錄內(nèi)容
  public void restoreFromMemento(INarrowMemento memento) {
    System.out.println("恢復備忘錄中的狀態(tài)...");
    if (memento != null) {
      Memento memento2 = (Memento) memento;
      this.blood = memento2.getBlood();
      this.sword = memento2.getSword();
    }
  }
  /**
   * 挑戰(zhàn)BOSS
   */
  public int koBoss() {
    // 當血液值<=0 時 挑戰(zhàn)失敗 假設戰(zhàn)勝BOSS的概率為2%
    // 判斷時候還有血液值
    if (this.blood <= 0 || this.sword <= 0) {
      System.out.println(this.toString());
      System.out.println("挑戰(zhàn)BOSS失?。?);
      return -1;
    } else {
      // 獲得隨機數(shù)
      double win = Math.random();
      if (win <= 0.02) {
        System.out.println(this.toString());
        System.out.println("恭喜你,挑戰(zhàn)BOSS成功!");
        return 1;
      } else {
        System.out.println(this.toString());
        System.out.println("繼續(xù)攻擊BOSS...");
        // 隨機數(shù)減少血液值和武力值 繼續(xù)KO
        int blood_sub = random.nextInt(10);
        int sword_sub = random.nextInt(10);
        this.blood -= blood_sub;
        this.sword -= sword_sub;
        return 0;
      }
    }
  }
  @Override
  public String toString() {
    return "當前血液值:" + this.blood + " - 當前武力值:" + this.sword;
  }
  /**
   * 備忘錄(整個類都是私有的,只有發(fā)起者才能訪問)
   *
   * @author
   *
   */
  private class Memento implements INarrowMemento {
    // 血液值
    private final int blood;
    // 武力值
    private final int sword;
    // 構造方法初始化 內(nèi)容
    private Memento(int blood, int sword) {
      this.blood = blood;
      this.sword = sword;
    }
    private int getBlood() {
      return blood;
    }
    private int getSword() {
      return sword;
    }
  }
}

3.3備忘錄管理者一Caretaker

package com.demo.caretaker;
import com.demo.memento.INarrowMemento;
/**
 * 管理者
 *
 * @author
 *
 */
public class Caretaker {
  private INarrowMemento memento;
  /**
   * 獲得備忘錄對象
   *
   * @return
   */
  public INarrowMemento getMemento() {
    return memento;
  }
  /**
   * 保存?zhèn)渫泴ο?   *
   * @param memento
   */
  public void setMemento(INarrowMemento memento) {
    this.memento = memento;
  }
}

3.4讓英雄挑戰(zhàn)Boss一Client

package com.demo;
import com.demo.caretaker.Caretaker;
import com.demo.originator.Hero;
/**
 * 客戶端主應用程序
 *
 * @author
 *
 */
public class Client {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // 創(chuàng)建角色
    Hero hero = new Hero();
    // 創(chuàng)建管理者
    Caretaker caretaker = new Caretaker();
    // 保存挑戰(zhàn)前的狀態(tài)信息
    caretaker.setMemento(hero.createMemento());
    // 只有三次戰(zhàn)勝BOSS的機會
    int cnt = 1;
    // 挑戰(zhàn)BOSS結果
    int ko = -1;
    while (ko != 1 && cnt <= 3) {
      System.out
          .println("=============== 第" + cnt + "次挑戰(zhàn) ==============");
      // 開始挑戰(zhàn)BOSS
      ko = hero.koBoss();
      while (true) {
        if (ko == -1) {
          // 挑戰(zhàn)失敗 恢復到初始狀態(tài) 累加挑戰(zhàn)次數(shù)
          hero.restoreFromMemento(caretaker.getMemento());
          cnt += 1;
          break;
        } else if (ko == 0) {
          // 繼續(xù)挑戰(zhàn)
          ko = hero.koBoss();
        } else if (ko == 1) {
          // 挑戰(zhàn)成功!
          break;
        }
      }
    }
  }
}

4運行結果

創(chuàng)建備忘錄...
=============== 第1次挑戰(zhàn) ==============
當前血液值:100 - 當前武力值:100
繼續(xù)攻擊BOSS...
當前血液值:96 - 當前武力值:99
繼續(xù)攻擊BOSS...
當前血液值:90 - 當前武力值:98
繼續(xù)攻擊BOSS...
當前血液值:81 - 當前武力值:95
繼續(xù)攻擊BOSS...
當前血液值:78 - 當前武力值:93
繼續(xù)攻擊BOSS...
當前血液值:72 - 當前武力值:88
繼續(xù)攻擊BOSS...
當前血液值:64 - 當前武力值:85
繼續(xù)攻擊BOSS...
當前血液值:56 - 當前武力值:80
繼續(xù)攻擊BOSS...
當前血液值:49 - 當前武力值:73
繼續(xù)攻擊BOSS...
當前血液值:45 - 當前武力值:71
繼續(xù)攻擊BOSS...
當前血液值:37 - 當前武力值:68
繼續(xù)攻擊BOSS...
當前血液值:29 - 當前武力值:65
繼續(xù)攻擊BOSS...
當前血液值:20 - 當前武力值:59
繼續(xù)攻擊BOSS...
當前血液值:11 - 當前武力值:54
繼續(xù)攻擊BOSS...
當前血液值:9 - 當前武力值:52
繼續(xù)攻擊BOSS...
當前血液值:3 - 當前武力值:45
繼續(xù)攻擊BOSS...
當前血液值:-3 - 當前武力值:41
挑戰(zhàn)BOSS失?。?br /> 恢復備忘錄中的狀態(tài)...
=============== 第2次挑戰(zhàn) ==============
當前血液值:100 - 當前武力值:100
繼續(xù)攻擊BOSS...
當前血液值:96 - 當前武力值:95
繼續(xù)攻擊BOSS...
當前血液值:96 - 當前武力值:91
繼續(xù)攻擊BOSS...
當前血液值:88 - 當前武力值:82
繼續(xù)攻擊BOSS...
當前血液值:79 - 當前武力值:79
繼續(xù)攻擊BOSS...
當前血液值:76 - 當前武力值:72
繼續(xù)攻擊BOSS...
當前血液值:73 - 當前武力值:70
繼續(xù)攻擊BOSS...
當前血液值:72 - 當前武力值:66
繼續(xù)攻擊BOSS...
當前血液值:72 - 當前武力值:61
繼續(xù)攻擊BOSS...
當前血液值:72 - 當前武力值:58
繼續(xù)攻擊BOSS...
當前血液值:72 - 當前武力值:52
繼續(xù)攻擊BOSS...
當前血液值:63 - 當前武力值:51
繼續(xù)攻擊BOSS...
當前血液值:62 - 當前武力值:50
繼續(xù)攻擊BOSS...
當前血液值:54 - 當前武力值:41
繼續(xù)攻擊BOSS...
當前血液值:50 - 當前武力值:39
繼續(xù)攻擊BOSS...
當前血液值:47 - 當前武力值:39
繼續(xù)攻擊BOSS...
當前血液值:43 - 當前武力值:38
繼續(xù)攻擊BOSS...
當前血液值:37 - 當前武力值:36
繼續(xù)攻擊BOSS...
當前血液值:34 - 當前武力值:35
繼續(xù)攻擊BOSS...
當前血液值:32 - 當前武力值:27
繼續(xù)攻擊BOSS...
當前血液值:28 - 當前武力值:22
繼續(xù)攻擊BOSS...
當前血液值:26 - 當前武力值:15
繼續(xù)攻擊BOSS...
當前血液值:24 - 當前武力值:11
繼續(xù)攻擊BOSS...
當前血液值:19 - 當前武力值:3
繼續(xù)攻擊BOSS...
當前血液值:10 - 當前武力值:-3
挑戰(zhàn)BOSS失??!
恢復備忘錄中的狀態(tài)...
=============== 第3次挑戰(zhàn) ==============
當前血液值:100 - 當前武力值:100
繼續(xù)攻擊BOSS...
當前血液值:99 - 當前武力值:93
繼續(xù)攻擊BOSS...
當前血液值:98 - 當前武力值:84
繼續(xù)攻擊BOSS...
當前血液值:98 - 當前武力值:82
繼續(xù)攻擊BOSS...
當前血液值:95 - 當前武力值:76
繼續(xù)攻擊BOSS...
當前血液值:88 - 當前武力值:68
繼續(xù)攻擊BOSS...
當前血液值:81 - 當前武力值:64
繼續(xù)攻擊BOSS...
當前血液值:76 - 當前武力值:64
繼續(xù)攻擊BOSS...
當前血液值:67 - 當前武力值:64
恭喜你,挑戰(zhàn)BOSS成功!

三. 該模式設計原則

1封裝邊界的保持
2雙重接口實現(xiàn),保證安全性。

四. 使用場合

1需要在某一時刻恢復一個對象先前的狀態(tài)時。
2白箱備忘錄模式,需要在外部保存對象某一時刻的狀態(tài),但如果用一個接口來讓其他對象直接得到這些狀態(tài),將會暴露對象的實現(xiàn)細節(jié)并破壞對象的封裝性。
3黑箱備忘錄模式實現(xiàn)方式提供了雙重接口訪問機制,對發(fā)起者對象提供寬接口,而對發(fā)起者以外的對象提供窄接口,從而有效解決了封裝性和安全性。

五. 靜態(tài)類圖

1白箱備忘錄模式靜態(tài)類圖

Java使用備忘錄模式實現(xiàn)過關類游戲功能詳解

2黑箱備忘錄模式靜態(tài)類圖

Java使用備忘錄模式實現(xiàn)過關類游戲功能詳解

更多java相關內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI