/// <summary> /// 复合享元 /// </summary> static void Flyweight() { Console.WriteLine($"复合享元{Environment.NewLine}-------------------------------------------"); IShape circleA = FlyweightFactory.Instance.CreateCircle("很圆的圆形B"); IShape circleB = FlyweightFactory.Instance.CreateCircle("很圆的圆形B"); IShape rectangleA = FlyweightFactory.Instance.CreateRectangle("很方的长方形B"); Console.WriteLine("--------------"); IShape shapCompositeA = FlyweightFactory.Instance.CreateComposite(); shapCompositeA.Add(circleA); shapCompositeA.Add(rectangleA); shapCompositeA.Draw(ConsoleColor.Yellow, 10, 130); Console.WriteLine("--------------"); IShape shapCompositeB = FlyweightFactory.Instance.CreateComposite(); shapCompositeB.Add(circleA); shapCompositeB.Add(circleB); shapCompositeB.Add(rectangleA); shapCompositeB.Draw(ConsoleColor.Blue, 25, 60); Console.WriteLine("--------------"); Console.WriteLine($"享元池长度:{FlyweightFactory.Instance.PoolSize}"); Console.WriteLine($"shapCompositeA与shapCompositeB是否指向同一内存地址:{object.ReferenceEquals(shapCompositeA, shapCompositeB)}"); Console.ReadKey(); }