欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java基于享元模式實(shí)現(xiàn)五子棋游戲功能實(shí)例詳解

 更新時(shí)間:2018年05月15日 11:40:57   作者:chengqiuming  
這篇文章主要介紹了Java基于享元模式實(shí)現(xiàn)五子棋游戲功能,較為詳細(xì)的分析了享元模式的概念、功能并結(jié)合實(shí)例形式詳細(xì)分析了Java使用享元模式實(shí)現(xiàn)五子棋游戲的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了Java基于享元模式實(shí)現(xiàn)五子棋游戲功能。分享給大家供大家參考,具體如下:

一、模式定義

享元模式,以共享的方式高效地支持大量的細(xì)粒度對(duì)象。通過復(fù)用內(nèi)存中已存在的對(duì)象,降低系統(tǒng)創(chuàng)建對(duì)象實(shí)例的性能消耗。享元的英文是Flyweight,表示特別小的對(duì)象,即細(xì)粒度對(duì)象。

二、模式舉例

1. 模式分析

我們借用五子棋游戲來(lái)說明這一模式。

2. 享元模式靜態(tài)類圖

3. 代碼示例

3.1 創(chuàng)建抽象棋子一AbstractChessman

package com.demo.flyweight.object;
public abstract class AbstractChessman {
  // 棋子坐標(biāo)
  protected int x;
  protected int y;
  // 棋子類別(黑|白)
  protected String chess;
  public AbstractChessman(String chess) {
    this.chess = chess;
  }
  // 點(diǎn)坐標(biāo)設(shè)置
  public abstract void point(int x, int y);
  // 顯示棋子信息
  public void show() {
    System.out.println(this.chess + "(" + this.x + "," + this.y + ")");
  }
}

3.2 創(chuàng)建黑子一BlackChessman

package com.demo.flyweight.object;
public class BlackChessman extends AbstractChessman {
  /**
   * 構(gòu)造方法 初始化黑棋子
   */
  public BlackChessman() {
    super("●");
    System.out.println("--BlackChessman Construction Exec!!!");
  }
  // 點(diǎn)坐標(biāo)設(shè)置
  @Override
  public void point(int x, int y) {
    this.x = x;
    this.y = y;
    // 顯示棋子內(nèi)容
    show();
  }
}

3.3 創(chuàng)建白子一WhiteChessman

package com.demo.flyweight.object;
public class WhiteChessman extends AbstractChessman {
  /**
   * 構(gòu)造方法 初始化白棋子
   */
  public WhiteChessman() {
    super("○");
    System.out.println("--WhiteChessman Construction Exec!!!");
  }
  // 點(diǎn)坐標(biāo)設(shè)置
  @Override
  public void point(int x, int y) {
    this.x = x;
    this.y = y;
    // 顯示棋子內(nèi)容
    show();
  }
}

3.4 創(chuàng)建棋子工廠一FiveChessmanFactory

package com.demo.flyweight.factory;
import java.util.Hashtable;
import com.demo.flyweight.object.AbstractChessman;
import com.demo.flyweight.object.BlackChessman;
import com.demo.flyweight.object.WhiteChessman;
public class FiveChessmanFactory {
  // 單例模式工廠
  private static FiveChessmanFactory fiveChessmanFactory = new FiveChessmanFactory();
  // 緩存存放共享對(duì)象
  private final Hashtable<Character, AbstractChessman> cache = new Hashtable<Character, AbstractChessman>();
  // 私有化構(gòu)造方法
  private FiveChessmanFactory() {
  }
  // 獲得單例工廠
  public static FiveChessmanFactory getInstance() {
    return fiveChessmanFactory;
  }
  /**
   * 根據(jù)字符獲得棋子
   *
   * @param c
   *      (B:黑棋 W:白棋)
   * @return
   */
  public AbstractChessman getChessmanObject(char c) {
    // 從緩存中獲得棋子對(duì)象實(shí)例
    AbstractChessman abstractChessman = this.cache.get(c);
    if (abstractChessman == null) {
      // 緩存中沒有棋子對(duì)象實(shí)例信息 則創(chuàng)建棋子對(duì)象實(shí)例 并放入緩存
      switch (c) {
      case 'B':
        abstractChessman = new BlackChessman();
        break;
      case 'W':
        abstractChessman = new WhiteChessman();
        break;
      default:
        break;
      }
      // 為防止 非法字符的進(jìn)入 返回null
      if (abstractChessman != null) {
        // 放入緩存
        this.cache.put(c, abstractChessman);
      }
    }
    // 如果緩存中存在 棋子對(duì)象則直接返回
    return abstractChessman;
  }
}

3.5 客戶端實(shí)現(xiàn)一Client

package com.demo;
import java.util.Random;
import com.demo.flyweight.factory.FiveChessmanFactory;
import com.demo.flyweight.object.AbstractChessman;
/**
 * 主應(yīng)用程序
 *
 * @author
 */
public class Client {
  /**
   * @param args
   */
  public static void main(String[] args) {
    // 創(chuàng)建五子棋工廠
    FiveChessmanFactory fiveChessmanFactory = FiveChessmanFactory
        .getInstance();
    Random random = new Random();
    int radom = 0;
    AbstractChessman abstractChessman = null;
    // 隨機(jī)獲得棋子
    for (int i = 0; i < 10; i++) {
      radom = random.nextInt(2);
      switch (radom) {
      // 獲得黑棋
      case 0:
        abstractChessman = fiveChessmanFactory.getChessmanObject('B');
        break;
      // 獲得白棋
      case 1:
        abstractChessman = fiveChessmanFactory.getChessmanObject('W');
        break;
      }
      if (abstractChessman != null) {
        abstractChessman.point(i, random.nextInt(15));
      }
    }
  }
}

4. 運(yùn)行結(jié)果

--WhiteChessman Construction Exec!!!

○(0,2)

○(1,6)

--BlackChessman Construction Exec!!!

●(2,3)

○(3,14)

○(4,13)

○(5,8)

●(6,14)

●(7,0)

●(8,3)

○(9,8)

三、享元模式的兩種狀態(tài)

內(nèi)蘊(yùn)狀態(tài)不會(huì)隨環(huán)境的改變而改變,是存儲(chǔ)在享元對(duì)象內(nèi)部狀態(tài)信息,困此內(nèi)蘊(yùn)狀態(tài)是可以共享的,對(duì)于任何一個(gè)享元對(duì)象來(lái)講,它的值是完全相同的。就像五子棋中的"黑子"和"白子",它代表的狀態(tài)就是內(nèi)蘊(yùn)狀態(tài)。

外蘊(yùn)狀態(tài)它會(huì)隨環(huán)境的改變而改變,因此不可以共享狀態(tài),對(duì)于不同的享元對(duì)象講,它的值可能是不同的。享元對(duì)象的外蘊(yùn)狀態(tài)必須由客戶端保存,在享元對(duì)象被創(chuàng)建之后,需要使用的時(shí)候再傳入享元對(duì)象內(nèi)部。就像五子棋的位置信息,代表的狀態(tài)就是享元對(duì)象的外蘊(yùn)狀態(tài)。

所以,享元的外蘊(yùn)狀態(tài)和內(nèi)蘊(yùn)狀態(tài)是兩類相互獨(dú)立的狀態(tài),彼此沒關(guān)聯(lián)。

四、該模式設(shè)計(jì)原則

1. 共享細(xì)粒度對(duì)象,降低內(nèi)存空間。

2. 有效地隔離系統(tǒng)中變化部分和不變部分。

五、使用場(chǎng)合

1. 當(dāng)系統(tǒng)中某個(gè)對(duì)象類型的實(shí)例較多的時(shí)候。

2. 在系統(tǒng)設(shè)計(jì)中,對(duì)象實(shí)例進(jìn)行分類后,發(fā)現(xiàn)真正有區(qū)別的分類很少的時(shí)候。

六、享元模式靜態(tài)類圖

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

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論