Exemplo n.º 1
0
        static void Main(string[] args)
        {
            // canonical example
            int extrinsicState = 20;

            var factory = new FlyweightFactory();

            var fa = factory.GetFlyWeight("A");

            fa.Operation(extrinsicState--);

            var fb = factory.GetFlyWeight("B");

            fb.Operation(extrinsicState--);

            var unsh = new UnsharedConcreteFlyweight();

            unsh.Operation(extrinsicState--);

            Console.WriteLine();

            // live example
            var fact = new CharacterFactory();
            var cha  = fact.GetCharacter('a');

            cha.Display(100);
        }
Exemplo n.º 2
0
        public static void AddCarToPoliceDatabase(FlyweightFactory factory, Car car)
        {
            Console.WriteLine($"{Environment.NewLine}Client: Adding a car to database.");

            var flyweight = factory.GetFlyweight(new Car
            {
                Color   = car.Color,
                Model   = car.Model,
                Company = car.Company
            });

            // Клиентский код либо сохраняет, либо вычисляет внешнее состояние и
            // передает его методам легковеса.
            flyweight.Operation(car);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var factory = new FlyweightFactory(
                new Car {
                Company = "Chevrolet", Model = "Camaro2018", Color = "pink"
            },
                new Car {
                Company = "Mercedes Benz", Model = "C300", Color = "black"
            },
                new Car {
                Company = "Mercedes Benz", Model = "C500", Color = "red"
            },
                new Car {
                Company = "BMW", Model = "M5", Color = "red"
            },
                new Car {
                Company = "BMW", Model = "X6", Color = "white"
            }
                );

            factory.ListFlyweights();

            AddCarToPoliceDatabase(factory, new Car
            {
                Number  = "CL234IR",
                Owner   = "James Doe",
                Company = "BMW",
                Model   = "M5",
                Color   = "red"
            });

            AddCarToPoliceDatabase(factory, new Car
            {
                Number  = "CL234IR",
                Owner   = "James Doe",
                Company = "BMW",
                Model   = "X1",
                Color   = "red"
            });

            factory.ListFlyweights();

            Console.ReadLine();
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            int extrinsicstate  = 20;
            FlyweightFactory f  = new FlyweightFactory();
            Flyweight        fx = f.GetFlyweight("X");

            fx.Operation(--extrinsicstate);
            Flyweight fy = f.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);
            Flyweight fz = f.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);
            Flyweight uf = new UnshareConcreteFlyweight();

            uf.Operation(--extrinsicstate);

            Console.ReadKey();
        }
Exemplo n.º 5
0
        public void DrawWithFlyweight()
        {
            var states      = new List <Bullet.ExtrinsicState>();
            var bulletTypes = new List <Bullet.BulletType>();

            for (int i = 0; i < 100000; i++)
            {
                states.Add(Bullet.GetRandomExtrinsicState());
                bulletTypes.Add(Bullet.GetRandomBulletType());
            }

            Bullet bullet;

            for (int i = 0; i < 100000; i++)
            {
                bullet         = FlyweightFactory.GetFlyweight(bulletTypes[i]);
                bullet.ExState = states[i];
                bullet.Draw();
            }
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            //享元模式:运用共享技术有效的支持大量细粒度的对象

            //初始化外部状态类,初始化两个用户
            User user1 = new User("小明");
            User user2 = new User("小红");

            //初始化一个享元工厂
            FlyweightFactory f   = new FlyweightFactory();
            Flyweight        fw1 = f.GetFlyweight(user1);

            fw1.Operation(1);
            Flyweight fw2 = f.GetFlyweight(user2);

            fw2.Operation(2);

            Flyweight fw3 = f.GetFlyweight(user1);

            fw3.Operation(3);

            Console.Read();
        }
Exemplo n.º 7
0
        /// <summary>
        /// The test second.
        /// </summary>
        private static void TestSecond()
        {
            // Arbitrary extrinsic state
            int extrinsicstate = 22;
            FlyweightFactory flyweightFactory = new FlyweightFactory();

            // Work with different flyweight instances
            AbstractFlyweight fx = flyweightFactory.GetFlyweight("X");

            fx.Operation(--extrinsicstate);
            AbstractFlyweight fy = flyweightFactory.GetFlyweight("Y");

            fy.Operation(--extrinsicstate);
            AbstractFlyweight fz = flyweightFactory.GetFlyweight("Z");

            fz.Operation(--extrinsicstate);
            UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();

            fu.Operation(--extrinsicstate);

            // Wait for user
            Console.ReadKey();
        }
Exemplo n.º 8
0
        static void Main(string[] args)
        {
            int i = 0;

            List <int> Americana = new List <int>();
            List <int> Italiana  = new List <int>();
            List <int> Mexicana  = new List <int>();

            List <int> Carnes    = new List <int>();
            List <int> Sopas     = new List <int>();
            List <int> Ensaladas = new List <int>();

            List <int> Rapida = new List <int>();

            FlyweightFactory flywf = new FlyweightFactory();

            i = flywf.Adiciona("Hamburguesa");
            Americana.Add(i);
            Carnes.Add(i);
            Rapida.Add(i);

            i = flywf.Adiciona("Wisconsin cheese");
            Americana.Add(i);
            Ensaladas.Add(i);

            i = flywf.Adiciona("Minestrone");
            Italiana.Add(i);
            Sopas.Add(i);

            i = flywf.Adiciona("Antipasto");
            Italiana.Add(i);
            Ensaladas.Add(i);

            i = flywf.Adiciona("Tacos al pastor");
            Mexicana.Add(i);
            Carnes.Add(i);
            Rapida.Add(i);

            i = flywf.Adiciona("Coditos");
            Mexicana.Add(i);
            Sopas.Add(i);

            i = flywf.Adiciona("Nopales");
            Mexicana.Add(i);
            Sopas.Add(i);

            i = flywf.Adiciona("Pizza");
            Italiana.Add(i);
            Rapida.Add(i);

            foreach (int n in Rapida)
            {
                Receta receta = (Receta)flywf[n];
                receta.CalculaCosto();
                receta.Mostrar();
            }

            Console.WriteLine("-----");

            foreach (int n in Americana)
            {
                Receta receta = (Receta)flywf[n];
                receta.CalculaCosto();
                receta.Mostrar();
            }

            Console.WriteLine("-----");

            i = flywf.Adiciona("Pizza");

            Console.WriteLine("-----");

            foreach (int n in Ensaladas)
            {
                Receta receta = (Receta)flywf[n];
                receta.CalculaCosto();
                receta.Mostrar();
            }
        }