享元模式的理解:
享元模式的定义:运用共享技术支持大量细粒度对象的复用;
Flyweight Pattern Definition:Use sharing to support large numbers of fine-grained efficiently.
享元模式关键词:大量、细粒度、复用、享元池、享元工厂;
类图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();         }     }运行结果
