static void Main(string[] args)
        {
            Console.WriteLine("Are you an adult or a child? A = Adult , C = Child ");
            string        input = Console.ReadLine();
            RecipeFactory factory;

            switch (input)
            {
            case "A":
                factory = new AdultCuisineFactory();
                break;

            case "C":
                factory = new KidCuisineFactory();
                break;

            default:
                Console.WriteLine($"What character was that? Are you an alien since you pressed {input}?");
                factory = new NoSandwich();
                //Man kunne også smide en exception men det er ikke sjovt
                break;
            }
            var sandwich = factory.CreateSandwich();
            var desert   = factory.CreateDessert();

            Console.WriteLine("Your sandwich is : " + sandwich.GetType().Name);
            Console.WriteLine("Your Dessert is : " + desert.GetType().Name);
            Console.ReadKey();
        }
Exemple #2
0
        /// <summary>
        /// The Abstract Factory pattern provides an interface for creating related families of objects
        /// without needing to specify the concrete implementations.  This pattern is critical for ideas
        /// like Dependency Injection.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Who are you? (A)dult or (C)hild?");
            char          input = Console.ReadKey().KeyChar;
            RecipeFactory factory;

            switch (input)
            {
            case 'A':
                factory = new AdultCuisineFactory();
                break;

            case 'C':
                factory = new KidCuisineFactory();
                break;

            default:
                throw new NotImplementedException();
            }

            var sandwich = factory.CreateSandwich();
            var dessert  = factory.CreateDessert();

            Console.WriteLine("\nSandwich: " + sandwich.GetType().Name);
            Console.WriteLine("Dessert: " + dessert.GetType().Name);

            Console.ReadKey();
        }