本文介绍了Java装饰图案比萨打顶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现具有4种不同浇头(Salami,Soudjouk,Onion,Pepper)的Pizza(美国和那不勒斯)装饰图案,并扩展 TopingDecorator类,并通过 Add Pizza命令将其中3种添加到Pizza中。但是,代码没有将其添加到Pizza的TopingDecorator ArrayList中。应该类似于以下内容(我正在将Salami和Soudjouk添加到AmericanPan Pizza(扩展了PlainPizza类)):

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

这是我的PlainPizza课:

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() {

    }

这是我的AmericanPan班:

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

我正在尝试在Salami类的American Pan上添加Salami:

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


推荐答案

您将要解决所有这些错误,装饰器模式,您可以使用不同的装饰器类来创建不同类型的实例。在您的情况下,这意味着您无法将多个浇头添加到比萨饼中,因为浇头本身就是比萨饼,因此,萨拉米香肠是萨拉米香肠比萨饼,而胡椒粉是胡椒比萨饼,而不是两个浇头

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

如果要向一个比萨饼添加多个浇头,则Decorator不是正确的模式。

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

这篇关于Java装饰图案比萨打顶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:22