設計模式之:享元模式FlyweightPattern的實現

張德長發表於2022-04-03

享元模式的理解:

享元模式的定義:運用共享技術支援大量細粒度物件的複用;

Flyweight Pattern Definition:Use sharing to support large numbers of fine-grained efficiently.

享元模式關鍵詞:大量、細粒度、複用、享元池、享元工廠

  1. 當系統中存在大量的細粒度的相同或相似物件時,可以使用享元模式;
  2. 享元模式通過共享技術,實現相同或相似物件的重複利用;
  3. 享元的字面理解:也就是分享共享的意思,就是共享的元素、物件;
  4. Flyweight的字面理解:Flyweight本意是拳擊運動的一個術語,就是蠅量級的意思,flyweight 蠅量級 112磅;
  5. 享元模式也叫輕量級模式,享元是對Flyweight的意譯,直譯的話應該叫蠅量級模式;
  6. 英文定義中採用Flyweight,是想表達物件的粒度,也就是fine-grained細粒度的意思;
  7. grain本意表示穀物,grained表示像穀物那種顆粒狀態,即粒度,而fine-grained則表示細粒度,例如fine grained soil 細土、coarse grained soil粗粒土;
  8. 享元模式和Unity中的預製體作用類似,享元模式可以通過共享元素生成多個物件,Unity同樣可以通過Prefabs生成成千上萬的怪物;
  9. 還有諸如物件池、執行緒池,實際上也是享元模式的使用案例;

類圖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();
        }
    }

 

執行結果

 

相關文章