samedi 20 avril 2019

How to remove decorated object from Decorator Pattern?

How could I remove items that have been added using the decorator design pattern? For example Pizza Ordering System. I want to be able delete some items and decrease price.

public interface Pizza {
    double getPrice();
}
public class SimplePizza implements Pizza {

    @Override
    public double getPrice() {
        return 25;
    }
}
public abstract class PizzaDecorator implements Pizza {
   Pizza tempPizza;
   public PizzaDecorator(Pizza a){
       tempPizza = a;
   }
   @Override
   public double getPrice(){
       return tempPizza.getPrice();
   }
}
public class Mushroom extends PizzaDecorator {
    public Mushroom(Pizza newPizza){
        super(newPizza);
    }
    public double getPrice(){
        return tempPizza.getPrice() + 10;
    }
}

Aucun commentaire:

Enregistrer un commentaire