享元模式的理解: 享元模式的定義:運用共用技術支持大量細粒度對象的復用; Flyweight Pattern Definition:Use sharing to support large numbers of fine-grained efficiently. 享元模式關鍵詞:大量、細粒度、復用、 ...
享元模式的理解:
享元模式的定義:運用共用技術支持大量細粒度對象的復用;
Flyweight Pattern Definition:Use sharing to support large numbers of fine-grained efficiently.
享元模式關鍵詞:大量、細粒度、復用、享元池、享元工廠;
- 當系統中存在大量的細粒度的相同或相似對象時,可以使用享元模式;
- 享元模式通過共用技術,實現相同或相似對象的重覆利用;
- 享元的字面理解:享也就是分享共用的意思,元就是共用的元素、對象;
- Flyweight的字面理解:Flyweight本意是拳擊運動的一個術語,就是蠅量級的意思,flyweight 蠅量級 112磅;
- 享元模式也叫輕量級模式,享元是對Flyweight的意譯,直譯的話應該叫蠅量級模式;
- 英文定義中採用Flyweight,是想表達對象的粒度,也就是fine-grained細粒度的意思;
- grain本意表示穀物,grained表示像穀物那種顆粒狀態,即粒度,而fine-grained則表示細粒度,例如fine grained soil 細土、coarse grained soil粗粒土;
- 享元模式和Unity中的預製體作用類似,享元模式可以通過共用元素生成多個對象,Unity同樣可以通過Prefabs生成成千上萬的怪物;
- 還有諸如對象池、線程池,實際上也是享元模式的使用案例;
類圖with StarUML
棋子抽象類和2個實現類
internal abstract class Chessman
{
public abstract string GetColor();
public void Display() { Console.WriteLine($"棋子顏色{this.GetColor()}"); }
}
internal class BlackChessman : Chessman
{
public override string GetColor() { return "黑色"; }
}
internal class WhiteChessman : Chessman
{
public override string GetColor() { return "白色"; }
}
享元工廠類
internal class ChessmanFactory
{
//餓漢式單例模式
private static ChessmanFactory instance = new ChessmanFactory();
//該字典相當於享元池(對象池)Flyweight Pool
private Dictionary<string, Chessman> dictionary;
//構造註入依賴項Chessman/BlackChessman/WhiteChessman
private ChessmanFactory()
{
dictionary = new Dictionary<string, Chessman>();
Chessman black = new BlackChessman();
Chessman white = new WhiteChessman();
dictionary.Add("b", black);
dictionary.Add("w", white);
}
//返回唯一實例
public static ChessmanFactory GetInstance() { return instance; }
//根據鍵是b還是w,返回字典中的對應棋子
public Chessman GetChessman(string color) { return dictionary[color]; }
}
客戶端
internal class Program
{
static void Main(string[] args)
{
Chessman black1, black2, white1, white2;
ChessmanFactory factory = ChessmanFactory.GetInstance();
//生成兩顆黑子,並比較
black1 = factory.GetChessman("b");
black2 = factory.GetChessman("b");
Console.WriteLine($"兩顆黑子是否相同?{black1 == black2}");
//生成兩顆白字,並比較
white1 = factory.GetChessman("w");
white2 = factory.GetChessman("w");
Console.WriteLine($"兩顆白子是否相同?{black1 == black2}");
//顯示棋子
black1.Display();
black2.Display();
white1.Display();
white2.Display();
Console.Read();
}
}
運行結果