Example #1
0
            /// <summary>

            /// Entry point into console application.

            /// </summary>

            public 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();
            }
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        private 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();
        }
Example #3
0
        public void Test()
        {
            var factory = new FlyweightFactory();

            factory["A"].Operation();
            factory["B"].Operation();
            factory["C"].Operation();
        }
Example #4
0
        public static void addCarToPoliceDatabase(FlyweightFactory factory, Car car)
        {
            Console.WriteLine("\nClient: Adding a car to database.");

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

            // The client code either stores or calculates extrinsic state and
            // passes it to the flyweight's methods.
            flyweight.Operation(car);
        }
Example #5
0
        static void Main(string[] args)
        {
            // The client code usually creates a bunch of pre-populated
            // flyweights in the initialization stage of the application.
            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();
        }
Example #6
0
        public void Test()
        {
            var flyweightFactory = new FlyweightFactory();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var flyweight1 = flyweightFactory.Get(TimeSpan.FromMilliseconds(200));
            stopwatch.Stop();

            Assert.That(flyweight1, Is.Not.Null);
            Assert.That(stopwatch.Elapsed, Is
                .GreaterThan(TimeSpan.FromMilliseconds(100))
                .And
                .LessThan(TimeSpan.FromMilliseconds(300)));

            stopwatch.Restart();
            var flyweight2 = flyweightFactory.Get(TimeSpan.FromMilliseconds(200));
            stopwatch.Stop();

            Assert.That(flyweight2, Is.Not.Null);
            Assert.That(stopwatch.Elapsed, Is
                .LessThan(TimeSpan.FromMilliseconds(1)));

            Assert.That(flyweight1, Is.SameAs(flyweight2));
            Assert.That(flyweight1.ShareableState, Is.SameAs(flyweight2.ShareableState));

            var flyweight3 = flyweightFactory.Get(TimeSpan.Zero);

            Assert.That(flyweight3, Is.Not.Null);
            Assert.That(flyweight3, Is.Not.SameAs(flyweight1));

            var response1 = flyweight1.Method(new Flyweight.ExtrinsicState {Content = "content 1"});
            var response2 = flyweight1.Method(new Flyweight.ExtrinsicState {Content = "content 2"});

            Assert.That(response1, Is.EqualTo("[00:00:00.2000000] content 1"));
            Assert.That(response2, Is.EqualTo("[00:00:00.2000000] content 2"));
        }