享元模式(運用共享技術有效的支持大量細粒度的物件)
- 優點:大大減少物件的創建,降低系統的記憶體,使效率提高,
- 缺點:提高了系統的復雜度,需要分離出內蘊狀態和外蘊狀態,而且外蘊狀態具有固有化的性質,不應該隨著內蘊狀態的變化而變化,否則會造成系統的混亂,
- 享元模式的定義提出了兩個要求,細粒度和共享物件,因為要求細粒度,所以不可避免地會使物件數量多且性質相近,此時我們就將這些物件的資訊分為兩個部分:內部狀態和外部狀態,
內蘊狀態:物件共享出來的資訊,存盤在享元物件內部并且不會隨環境的改變而改變,
外蘊狀態:物件得以依賴的一個標記,是隨環境改變而改變的、不可共享的狀態, - 享元模式的主要角色
抽象享元角色:為具體享元角色規定了必須實作的方法,非享元的外蘊狀態以引數的形式通過此方法傳入,
具體享元角色:實作抽象享元角色的介面,
享元工廠角色:負責創建和管理享元角色,當客戶物件請求一個享元物件時,享元工廠檢查系統中是否存在符合要求的享元物件,如果存在則提供給客戶;如果不存在,則創建一個新的享元物件,
非享元角色:是不可以共享的外蘊狀態,它以引數的形式注入具體享元的相關方法中, - 例子:圍棋只有黑白兩色,所以棋子顏色就是棋子的內蘊狀態;而各個棋子之間的差別就是位置的不同,我們落子嘛,落子顏色是定的,但位置是變化的,所以方位坐標就是棋子的外蘊狀態,類圖如下:
- 代碼如下:
非享元角色
/**
* 棋盤位置(外蘊狀態)
*/
public class Location {
private int x;
private int y;
public Location(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
抽象享元角色
/**
* 棋子
*/
public interface ChessPieces {
void downPieces(Location location);
}
具體享元角色
/**
* 白棋(內蘊狀態)
*/
public class WhitePieces implements ChessPieces {
public WhitePieces() {
System.out.println("+++生成白棋物件+++");
}
@Override
public void downPieces(Location location) {
System.out.println("白棋物件已經存在,被成功獲取!");
System.out.println("坐標X="+location.getX()+";Y="+location.getY());
}
}
/**
* 黑棋(內蘊狀態)
*/
public class BlackPieces implements ChessPieces {
public BlackPieces() {
System.out.println("+++生成黑棋物件+++");
}
@Override
public void downPieces(Location location) {
System.out.println("黑棋物件已經存在,被成功獲取!");
System.out.println("坐標X="+location.getX()+";Y="+location.getY());
}
}
享元工廠角色
/**
* 工廠
*/
public class PiecesFactory {
private static final String WRITE = "白棋";
private static final String BLACK = "黑棋";
Map<String, ChessPieces> pieces = new HashMap<>();
// 獲取棋子物件
public ChessPieces getPieceInstance(String color) {
if(pieces.get(color) == null) {
if(color == WRITE) {
WhitePieces whitePieces = new WhitePieces();
pieces.put(color, whitePieces);
}else if(color == BLACK){
BlackPieces blackPieces = new BlackPieces();
pieces.put(color, blackPieces);
}else {
System.out.println("不存在的顏色");
return null;
}
}
return pieces.get(color);
}
}
測驗
public class Test {
private static final String WRITE = "白棋";
private static final String BLACK = "黑棋";
public static void main(String[] args) {
try {
PiecesFactory factory = new PiecesFactory();
System.out.println("---游戲開始---");
String color = WRITE;
while (true) {
if (color.equals(WRITE)) {
System.out.println("---白棋請落子---");
} else {
System.out.println("---黑棋請落子---");
}
System.out.println("請輸入X坐標");
Scanner scanner1 = new Scanner(System.in);
int x = scanner1.nextInt();
System.out.println("請輸入y坐標");
Scanner scanner2 = new Scanner(System.in);
int y = scanner2.nextInt();
ChessPieces chessPieces = factory.getPieceInstance(color);
chessPieces.downPieces(new Location(x, y));
if (WRITE.equals(color)) {
color = BLACK;
} else {
color = WRITE;
}
}
} catch (Exception e) {
System.out.println("---游戲結束---");
}
}
}
// 運行結果
---游戲開始---
---白棋請落子---
請輸入X坐標
1
請輸入y坐標
2
+++生成白棋物件+++
白棋物件已經存在,被成功獲取!
坐標X=1;Y=2
---黑棋請落子---
請輸入X坐標
4
請輸入y坐標
5
+++生成黑棋物件+++
黑棋物件已經存在,被成功獲取!
坐標X=4;Y=5
---白棋請落子---
請輸入X坐標
3
請輸入y坐標
3
白棋物件已經存在,被成功獲取!
坐標X=3;Y=3
---黑棋請落子---
請輸入X坐標
5
請輸入y坐標
5
黑棋物件已經存在,被成功獲取!
坐標X=5;Y=5
---白棋請落子---
請輸入X坐標
eee
---游戲結束---
運行上面代碼,可以發現,在一局游戲中,只會在一開始生成白棋和黑棋物件兩個物件,后面的落子只需要在工廠之中獲取就好,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/295748.html
標籤:其他
上一篇:Unity較新版本如何有效洗掉Hierarchy下的克隆體
下一篇:Devc++戰斗1.0