jeudi 3 septembre 2015

is the decorator pattern really necessary in this example?

Here is the problem statement

A restaurant has 4 Pizza bases:

Whole Wheat Pizza
Wood Fire Pizza
Cheese Filled Pizza
Thin crust Pizza

There are 8 toppings:

Tomato, Onion, Cheese, Pepporoni, Capsicum, Garlic, paneer, potato,

Calculate price of Pizza given a pizza base and 0 or more toppings with it (assuming each base and topping has some price configured).

My pseudocode solution to this was:

    public class Pizza {
        public int getPrice(base, String... toppings){
            PizzaBaseMap.get(base) + toppingsMap.sum(t -> toppingsMap.get(t))
        }



    Hashmap<String, int> PizzaBaseMap= {
        whole_wheat : 1
        wood_fire : 2
        cheese_filled : 2
        thin_crust : 4
    }

    Hashmap<String, int> toppingsMap = {
        tomato : 1
        onion : 2
        cheese : 4
        pepporoni : 5
        capsicum : 2
        garlic : 2
        paneer : 4
        potato : 4
    }

    //client Code 

    int Price = new Pizza().getPrice("whole_wheat", ["tomato", "cheese"])

Do I really need to use the decorator like the Headfirst desgin pattern book suggests in their example? The solution with the decorator pattern looks something like this:

    public interface iPizza
        {
             double cost();
        }

    //Decorator
    public interface iToppingDecorator:iPizza
    {
    }

    //Pizza types
    class WholeWheatPizza:iPizza
        {
            public double cost()
            {

            }
        }

    class WoodFire : iPizza
        {
            public double cost()
            {

            }
        }

     class CheeseFilled : iPizza
        {
            public double cost()
            {

            }
        }

      class Thincrust : iPizza
        {
            public double cost()
            {

            }
        }

    //Toppings inheriting Decorator Interface

    class CheeseTopping:iToppingDecorator
        {
             iPizza pizza;
             public CheeseTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost(); 
            }
        }

     class TomatoTopping:iToppingDecorator
        {
            iPizza pizza;
            public TomatoTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class OnionTopping:iToppingDecorator
        {
            iPizza pizza;
            public OnionTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class PepporoniTopping:iToppingDecorator
        {
            iPizza pizza;
            public PepporoniTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class CapsicumTopping:iToppingDecorator
        {
            iPizza pizza;
            public CapsicumTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class PaneerTopping:iToppingDecorator
        {
            iPizza pizza;
            public PaneerTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class GarlicTopping:iToppingDecorator
        {
            iPizza pizza;
            public GarlicTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

     class PotatoTopping:iToppingDecorator
        {
            iPizza pizza;
            public PotatoTopping(iPizza pizzatype)
            {
                this.pizza = pizzatype;
            }

            public double cost()
            {
                return <price> + pizza.cost();
            }
        }

    //client
    static void Main()
            {
                iPizza pizza1 = new WholeWheatPizza();
                pizza1 = new CheeseTopping(pizza1);

                Console.WriteLine("Pizza 1 cost: "+pizza1.cost()+"INR");

                iPizza pizza2 = new WoodFire();
                pizza2 = new CheeseTopping(pizza2);
                pizza2 = new TomatoTopping(pizza2);
                Console.WriteLine("Pizza 2 cost: " + pizza2.cost() + "INR");

                Console.ReadLine();

            }

I just feel it's a complete overkill, my code is just as extendible as the solution with the decorator pattern. Any thoughts?

Aucun commentaire:

Enregistrer un commentaire