Inheritance: Wincubate.FlyweightExamples.Coffee
Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            RandomNameGenerator rng = new RandomNameGenerator();

            for (int i = 0; i < 1000; i++)
            {
                ICoffee c1 = new Cappuccino(rng.GetRandomName());
                c1.Serve();
                ICoffee c2 = new Espresso(rng.GetRandomName());
                c2.Serve();
                ICoffee c3 = new LatteDoppio(rng.GetRandomName());
                c3.Serve();
                ICoffee c4 = new LatteGrande(rng.GetRandomName());
                c4.Serve();
                ICoffee c5 = new LatteTriplo(rng.GetRandomName());
                c5.Serve();
            }

            //Console.WriteLine();
            //Console.WriteLine( $"{Coffee.CoffeesCreated} objects created");
        }
        public ICoffee CreateCoffee(string coffeeType)
        {
            bool exists = _coffees.ContainsKey(coffeeType);

            if (exists == false)
            {
                // This could be done easier of course ;-)
                // See Module 02
                switch (coffeeType)
                {
                case nameof(Cappuccino):
                    _coffees[coffeeType] = new Cappuccino();
                    break;

                case nameof(Espresso):
                    _coffees[coffeeType] = new Espresso();
                    break;

                case nameof(LatteDoppio):
                    _coffees[coffeeType] = new LatteDoppio();
                    break;

                case nameof(LatteGrande):
                    _coffees[coffeeType] = new LatteGrande();
                    break;

                case nameof(LatteTriplo):
                    _coffees[coffeeType] = new LatteTriplo();
                    break;

                default:
                    throw new NotSupportedException($"Coffee type {coffeeType} not supported");
                }
            }

            return(_coffees[coffeeType]);
        }