lundi 15 avril 2019

How to write the addDough Method to my Pizza using the decorator DP

I'm creating three types of Pizza(Chicken, Pepperoni and Vegetarian) and each of them can have extra toppings which will increase the pizza price(like extra cheese, sausage...) so I know I need to use the decorator DP (I'm writing in eclipse IDE) but my problem is that I have 2 types of dough(Flatbread & Thin Crust) and each of my pizza SHOULD have a dough but I don't know where to add that dough part. Here's my Pizza interface:

public interface Pizza {
    public String getDescription();
    public double getCost();    
 }

Here's my Chicken Pizza Class(pepperoni and veg will be added later):

public class ChickenPizza implements Pizza {

    @Override
    public String getDescription() {

        return "Chicken";
    }
    @Override
    public double getCost() {

        return 10;
    }
}

My ToppingDecorator class:

public abstract class ToppingDecorator implements Pizza {
    protected Pizza tempPizza;
    public ToppingDecorator(Pizza newPizza) {
        tempPizza=newPizza;
    }

    @Override
    public String getDescription() {
        return tempPizza.getDescription();
    }

    @Override
    public double getCost() {
        return tempPizza.getCost();
    }
}

And my ExtraCheese Class(sausage and other toppings will be added later):

public class ExtraCheese extends ToppingDecorator{

    public ExtraCheese(Pizza newPizza) {
        super(newPizza);
    }


    public String getDescription() {
        return tempPizza.getDescription()+", Extra Cheese";
    }


    public double getCost() {
        return tempPizza.getCost()+2;
    }
}

So my question is: At first I thought that I should make 2 classes 1 for the Flatbread Dough and the other for the Thin Crust Dough(like I did to the Extra Cheese) but it won't change the cost, then I thought that I should make an abstract get and set methods in the Pizza interface but I'm not sure. Thank you.

Aucun commentaire:

Enregistrer un commentaire