public IHotDrink MakeDrink()
        {
            Console.WriteLine("Available Drinks");

            for (int i = 0; i < Factories.Count; i++)
            {
                Tuple <string, IHotDrinkFactory> entry = Factories[i];
                Console.WriteLine($"{i}: {entry.Item1}");
            }

            while (true)
            {
                string input = Console.ReadLine();

                if (int.TryParse(input, out int drinkOption) && drinkOption >= 0 && drinkOption <= Factories.Count)
                {
                    Console.WriteLine("Specify Amount");

                    input = Console.ReadLine();

                    if (int.TryParse(input, out int amount) && amount > 0)
                    {
                        IHotDrinkFactory hotDrinkFactory = Factories[drinkOption].Item2;
                        return(hotDrinkFactory.Prepare(amount));
                    }
                }
                else
                {
                    Console.WriteLine("Selected option is invalid");
                }
            }
        }
예제 #2
0
    public HotDrinkMachine()
    {
        //foreach (AvailableDrink drink in Enum.GetValues(typeof(AvailableDrink)))
        //{
        //    var factory = (IHotDrinkFactory)Activator.CreateInstance(Type.GetType(Enum.GetName(typeof(AvailableDrink), drink) + "Factory"));
        //   factories.Add(drink, factory);
        //}


        //foreach (var type in typeof(HotDrinkMachine).Assembly.GetTypes())


        Assembly assembly = typeof(HotDrinkMachine).Assembly;

        Debug.Log(assembly);
        foreach (var type in assembly.GetTypes())
        {
            Debug.Log(type);
            if (typeof(IHotDrinkFactory).IsAssignableFrom(type) && !type.IsInterface)
            {
                string name = type.Name.Replace("Factory", String.Empty);

                IHotDrinkFactory factory = (IHotDrinkFactory)Activator.CreateInstance(type);
                namedFactories.Add(new Factories(name, factory));
            }
        }
    }
예제 #3
0
 public HotDrinkMachine()
 {
     foreach (AvailableDrink drink in Enum.GetValues(typeof(AvailableDrink)))
     {
         IHotDrinkFactory factory = (IHotDrinkFactory)Activator.CreateInstance(Type.GetType("FactoryPattern." + Enum.GetName(typeof(AvailableDrink), drink) + "Factory"));
         factories.Add(drink, factory);
     }
 }
 public HotDrinkMachine()
 {
     foreach (AvailableDrink drink in Enum.GetValues(typeof(AvailableDrink)))
     {
         //Get's an instance of the interface implementation class using reflection and creates an instance of the class using the Activator
         IHotDrinkFactory factory = (IHotDrinkFactory)Activator.CreateInstance(Type.GetType("DesignPatterns.FactoryMethodAndAbstractFactoryPattern." + Enum.GetName(typeof(AvailableDrink), drink) + "Factory"));
         Factories.Add(drink, factory);
     }
 }
예제 #5
0
        public void Setup()
        {
            _provider = NSubstitute.Substitute.For <IServiceProvider>();

            _provider.GetService(Arg.Is <Type>(x => x == typeof(Chocolate))).Returns(new Chocolate());
            _provider.GetService(Arg.Is <Type>(x => x == typeof(Coffee))).Returns(new Coffee());
            _provider.GetService(Arg.Is <Type>(x => x == typeof(LemonTea))).Returns(new LemonTea());

            _factory = new HotDrinkFactory(_provider);
        }
        public HotDrinkMachine()
        {
            factoriesNameSpace = "DesignPatterns.GangOfFour.Creational.Factory.Abstract.Classes";

            //initialize the dicionary with the available drinks and its corresponding factory objects
            //at the runtime using reflection

            foreach (AvailableDrinks drink in Enum.GetValues(typeof(AvailableDrinks)))
            {
                IHotDrinkFactory factory = (IHotDrinkFactory)Activator
                                           .CreateInstance(Type.GetType($"{factoriesNameSpace}." +
                                                                        $"{Enum.GetName(typeof(AvailableDrinks),drink)}" +
                                                                        $"Factory"));

                factories.Add(drink, factory);
            }
        }
 public HotDrinkController(ILogger <HotDrinkController> logger, IHotDrinkFactory hotDrinkFactory)
 {
     _logger          = logger;
     _hotDrinkFactory = hotDrinkFactory;
 }
예제 #8
0
 public Factories(string name, IHotDrinkFactory factory)
 {
     this.name    = name;
     this.factory = factory;
 }