jeudi 23 mars 2017

Abstract factory method pattern with Ninject

This is a school project where I have an abstract class Store with two child classes:

public abstract class Store
{
    public IProcessedOrder ProcessOrder(IOrder order)
    {
       this.CreateCoffee(order.CoffeeType, order.Size)
       // Do stuff here
    }

    protected abstract IBeverage CreateCoffee(string coffeeType, BeverageSizeType size);
}

First child class:

public class StoreA : Store
{
    protected override IBeverage CreateCoffee(string coffeeType, BeverageSizeType size)
    {
        switch (coffeeType)
        {
            case "Americano": return new Americano(size);
            case "Capuccino": return new Capuccino(size);
            case "Espresso": return new Espresso(size);
            case "Latte": return new Latte(size);
            default:
                throw new ArgumentException();
        }
    }
}

The other child class looks the same but returns different coffee types.

This switch is bad implementation because if tomorrow I need another coffee type, I will have to come back to StoreA class and add new code. I would like to get rid of the switch and rather use Ninject for object composition. Since I'm quite new to Ninject (MVC as well) I can't say I fully understand what and how Ninject does exactly. Can you help how to bind this CreateCoffee method for both classes.

Aucun commentaire:

Enregistrer un commentaire