Java Decorative Pattern Pizza Topping

人走茶凉 提交于 2019-12-25 17:17:33

问题


i have to implement Pizza(American and Neapolitan) decoration pattern with 4 different toppings(Salami,Soudjouk,Onion,Pepper) which extends "TopingDecorator" class and out of them 3 will be added to pizza by "Add Pizza" command.However, the code does not add it to Pizza's TopingDecorator ArrayList. It should be something like below(I am trying to add Salami and Soudjouk to AmericanPan pizza(which extends PlainPizza class)):

AmericanPan a = new American();
Salami s = new Salami(a);
Soudjouk so = new Soudjouk(s);

Here is my PlainPizza class:

public class PlainPizza implements Pizza{

    private int cost;
    private String name;
    private int orderID;
    List<ToppingDecorator> topingsOfPizza;

    public PlainPizza(int orderID){
        this.orderID = orderID;
        topingsOfPizza = new ArrayList<ToppingDecorator>();
    }


    public void addPizza(PlainPizza p) {
        Pizza.allPizzas.add(p);

    }

    public List<ToppingDecorator> getTopingsOfPizza() {
        return topingsOfPizza;
    }

    @Override
    public int cost() {
        // TODO Auto-generated method stub
        return cost;
    }

    public int getOrderID() {
        return orderID;
    }

    public String getName() {
        return name;
    }


    @Override
    public void addTopping() {

    }

And here is my AmericanPan class:

public class AmericanPan extends PlainPizza{

    // Class Instances
    private final int cost = 5;
    private String name;

    // Constructor
    public AmericanPan(int orderID) {
        super(orderID);
        this.name = "AmericanPan";
    }

    // Get Cost
    @Override
    public int cost() {
        return cost;
    }

    // Get Name
    public String getName() {
        return name;
    }

I am tryin to add Salami on American Pan in Salami class:

public class Salami extends ToppingDecorator{

    private String name;
    ToppingDecorator t;

    public Salami(PlainPizza pizza) {
        super(pizza);
        this.name = "salami";
        this.addToping();
    }

    @Override
    public int cost() {
        return super.cost() + 3;
    }

    @Override
    public void addTopping() {
        t = new Salami(pizza);
        pizza.topingsOfPizza.add(t);

    }

And I am trying to add it with code below in my function in main class which operates the whole process:

PlainPizza piz = new AmericanPan(orderID);

// Check The Toppings that Pizza contains
if(pizzatops.contains("soudjouk")){
    soudjok = true;
}if(pizzatops.contains("salami")){
    salami = true;
}if(pizzatops.contains("pepper")){
    pepper = true;
}if(pizzatops.contains("onion")){
    onion = true;
}
// Add Pizza according to Toppings
for(int g = 0;g<pizzatops.size();g++){
    if(pizzatops.get(g).equals("salami")){
        Salami s = new Salami(piz);
    }else if(pizzatops.get(g).equals("pepper")){
        Pepper p = new Pepper(piz);
    }else if(pizzatops.get(g).equals("soudjouk")){
        Soudjouk p = new Soudjouk(piz);
    }
    else if(pizzatops.get(g).equals("onion")){
        Onion o = new Onion(piz);
    }
}
Pizza.allPizzas.add(piz);

System.out.println("AmericanPan pizza added to order " + orderID);

回答1:


You're going about this all wrong, with the decorator pattern you use different decorator classes to create different type of instances. In your case this means that you can't add multiple toppings to a pizza because the toppings are actually pizzas themselves, so Salami is a salami pizza and Pepper is a pepper pizza and not two toppings

If you want to add multiple toppings to one pizza then Decorator is not the right pattern.

Here is my simplified decorator implementation

interface Pizza {
  int cost();
}

public class PlainPizza implements Pizza {

  @Override
  public int cost() {
    return 10;
  }
}

public abstract class ToppingDecorator implements Pizza {
  private Pizza pizza;

  public ToppingDecorator(PlainPizza aPizza) {
    pizza = aPizza;
  }

  @Override
  public int cost() {
    return pizza.cost();
  }
}

public class SalamiPizza extends ToppingDecorator {
  public SalamiPizza(PlainPizza aPizza) {
    super(aPizza);
  }

  @Override
  public int cost() {
    return super.cost() +3;
  }
}

public static void main(String[] args) {
  SalamiPizza p = new SalamiPizza(new PlainPizza());
  System.out.print(p.cost());
}



回答2:


I tnink, your implementation is wrong. Decorator pattern using interfaces, abstract classes.

Here

You can see, what is the right implementation with Java.



来源:https://stackoverflow.com/questions/50036248/java-decorative-pattern-pizza-topping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!