Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 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();
            }
        }