コード例 #1
0
        public void SliderFlyWeight()
        {
            SliderFactory factory = new SliderFactory();

            Slider character = factory.GetSlider('B');

            string mensaje = character.Display(1);

            Assert.AreEqual("Slider #1: Bacon Master - topped with American cheese and lots of bacon! $" + 2.39m, mensaje);
        }
コード例 #2
0
        // Note that the ConcreteFlyweight classes are, of course, very similar to one another:
        // they all have the same properties.This is critical to using Flyweight: all of the related objects must have
        // the same definition(or at least reasonably close to the same definition).

        static void Main(string[] args)
        {
            #region Publication

            // Keen observers will probably already notice the problem:
            // The Author value we’re passing into our tuple creation is always a new Author()
            // instance in this example. Even though the string Name property value of the Author is the same in both cases,
            // the underlying Author object is different, and therefore, the generated key that is used for comparison
            // within the Library.GetPublication() method differs.

            // The solution is to explicitly pass the same instance of Author to both our Book retrieval attempts,
            // which we do here in Example2():
            Example1();
            Example2();
            Example3();

            #endregion

            #region Sliders

            // The Slider class has properties for Name, Cheese, Toppings, and Price
            // (all of which are part of the intrinsic state of these objects)

            // Build a slider order using patron's input
            Console.WriteLine("Please enter your slider order (use characters B, V, Z with no spaces):");
            var    order = Console.ReadLine();
            char[] chars = order.ToCharArray();

            SliderFactory factory = new SliderFactory();

            int orderTotal = 0;

            //Get the slider from the factory
            foreach (char c in chars)
            {
                orderTotal++;
                Slider character = factory.GetSlider(c);
                character.Display(orderTotal);
            }

            // Looking at the screenshot above, the FlyweightFactory will have only created new sliders for orders 1, 3, and 4,
            // with every other order being a copy of those objects.This is the power of Flyweight:
            // you can theoretically improve performance by only instantiating new objects on first creation.

            Console.ReadKey();

            #endregion
        }
コード例 #3
0
        public static void Flyweight()
        {
            Console.WriteLine("Please enter your slider order (use characters B, V, Z with no spaces):");
            var order = Console.ReadLine();

            char[] chars = order.ToCharArray();

            SliderFactory factory = new SliderFactory();

            int orderTotal = 0;

            //Get the slider from the factory
            foreach (char c in chars)
            {
                orderTotal++;
                Slider character = factory.GetSlider(c);
                character.Display(orderTotal);
            }
        }