Exemplo n.º 1
0
        static void Main(string[] args)
        {
            int i = 100;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight flyA = factory.GetFlyweight("A");

            flyA.Operation(i);

            Flyweight flyB = factory.GetFlyweight("B");

            flyB.Operation(i * 2);

            Flyweight flyC = factory.GetFlyweight("C");

            flyC.Operation(i * 3);

            Flyweight flyD = factory.GetFlyweight("D");

            flyD.Operation(i * 4);

            Flyweight flyE = new UnsharedFlyweight();

            flyE.Operation(i / 2);

            Flyweight flyF = factory.GetFlyweight("A");

            flyF.Operation(i / 2);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        static void Main()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;

            FlyweightFactory factory = new FlyweightFactory();

            // Work with different flyweight instances
            Flyweight fx = factory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);

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

            fy.Operation(--extrinsicstate);

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

            fz.Operation(--extrinsicstate);

            UnsharedConcreteFlyweight fu = new
                                           UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            // Wait for user
            Console.ReadKey();
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            FlyweightFactory factory    = new FlyweightFactory();
            Flyweight        flyweight1 = factory.GetFlyweight("related");

            flyweight1.Operation("hello world");
            Flyweight flyweight2 = factory.GetFlyweight("unrelated");

            flyweight2.Operation("hello second world");
            Flyweight flyweight3 = factory.GetFlyweight("related");

            flyweight3.Operation("hello super world");
            Flyweight flyweight4 = factory.GetFlyweight("unrelated");

            flyweight4.Operation("Bye");
        }
Exemplo n.º 4
0
        void Test()
        {
            // 外部状态
            int extrinsicState       = 22;
            FlyweightFactory factory = new FlyweightFactory();

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

            fx.Operation(--extrinsicState);

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

            fy.Operation(--extrinsicState);

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

            fz.Operation(--extrinsicState);

            Flyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(--extrinsicState);
        }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            FlyweightFactory flyweightFactory = new FlyweightFactory();

            Flyweight fA = flyweightFactory.GetFlyweight("A");

            fA.Operation(10);

            Flyweight fB = flyweightFactory.GetFlyweight("A");

            fB.Operation(9);

            Flyweight fC = flyweightFactory.GetFlyweight("C");

            fC.Operation(8);

            UnsharedConcreteFlyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(7);

            Console.WriteLine("fA == fB:" + (fA == fB));
            Console.WriteLine("fA == fC:" + (fA == fC));
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            int extrinsincstate = 22;
            FlyweightFactory f  = new FlyweightFactory();

            Flyweight fx = f.GetFlyweight("X");

            fx.Operation(extrinsincstate);

            Flyweight fy = f.GetFlyweight("Y");

            fy.Operation(extrinsincstate);

            Flyweight fz = f.GetFlyweight("Z");

            fz.Operation(extrinsincstate);

            Flyweight uf = new UnsharedConcreteFlyweight();

            uf.Operation(extrinsincstate);

            Console.ReadKey();
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            int extrinsicstate       = 12;
            FlyweightFactory factory = new FlyweightFactory();

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

            fx.Operation(--extrinsicstate);

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

            fy.Operation(--extrinsicstate);

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

            fz.Operation(--extrinsicstate);

            UnshardConcreteFlyweight uf = new UnshardConcreteFlyweight();

            uf.Operation(--extrinsicstate);


            Console.Read();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            int i = 100;
            FlyweightFactory factory = new FlyweightFactory();

            Flyweight flyA = factory.GetFlyweight("A");

            flyA.Operation(i);

            Flyweight flyB = factory.GetFlyweight("B");

            flyB.Operation(i * 2);

            Flyweight flyC = factory.GetFlyweight("C");

            flyC.Operation(i * 3);

            Flyweight flyD = factory.GetFlyweight("D");

            flyD.Operation(i * 4);

            Flyweight flyE = new UnsharedFlyweight();

            flyE.Operation(i / 2);

            Flyweight flyF = factory.GetFlyweight("A");

            flyF.Operation(i / 2);

            Console.WriteLine("================================");
            //共享图片工厂
            ImageNodeFactory imageNodeFactory = new ImageNodeFactory();

            Dictionary <int, ImageNode> imageList = new Dictionary <int, ImageNode>();
            int    randomNum = 0;
            Random random    = new Random();

            for (int j = 0; j < 10; j++)
            {
                randomNum = random.Next(0, 10);
                switch (randomNum / 2)
                {
                case 0:
                    imageList.Add(j, imageNodeFactory.GetImage("Lion"));
                    break;

                case 1:
                    imageList.Add(j, imageNodeFactory.GetImage("Tiger"));
                    break;

                case 2:
                    imageList.Add(j, imageNodeFactory.GetImage("Cat"));
                    break;

                default:
                    imageList.Add(j, imageNodeFactory.GetImage("Dog"));
                    break;
                }
            }

            //展示所生成的图像
            for (int j = 0; j < 10; j++)
            {
                Console.WriteLine(j + "号坐标的");
                imageList[i].Show();
            }

            //随机选取两个坐标的图像进行比较
            int a = 0;
            int b = 0;

            while (a == b)
            {
                a = random.Next(0, 10);
                b = random.Next(0, 10);
            }

            //由于使用了共享,所以直接判断对象是否相等即可
            if (imageList[a] == imageList[b])
            {
                Console.WriteLine("可以消除");
            }
            else
            {
                Console.WriteLine("不可以消除");
            }
        }