Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
151 views
Welcome To Ask or Share your Answers For Others

1 Answer

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());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...