コード例 #1
0
        static void Main(string[] args)
        {
            // Arbitrary extrinsic state

            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

            // Work with different flyweight instances

            IFlyweight fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

            IFlyweight fy = factory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);

            IFlyweight fz = factory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            // Wait for user

            Console.ReadKey();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            ///初始化享元工厂
            FlyweightFactory factory = new FlyweightFactory();

            while (true)
            {
                Console.WriteLine("请输入字符的位置:");
                var indexstring = Console.ReadLine();
                if (int.TryParse(indexstring, out int index))
                {
                    Console.WriteLine("请输入字符:");
                    string str = Console.ReadLine();

                    ///判断字符是否创建
                    Flyweight flyweight = factory.GetFlyweight(str);

                    //如果存在则输出信息
                    if (flyweight != null)
                    {
                        flyweight.OutInput(index);
                    }
                }
                else
                {
                    Console.WriteLine("请输入数字!");
                }
                Console.WriteLine("结束请输入N");
                if (Console.ReadLine() == "N")
                {
                    break;
                }
            }

            Console.WriteLine("已结束");
            Console.ReadLine();
        }