有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

面向对象Java装饰图案比萨面层

我必须实现披萨(美国和那不勒斯)装饰模式,使用4种不同的配料(意大利腊肠、苏德胡克、洋葱、胡椒),这扩展了“TopingDecorator”类,其中3种将通过“添加披萨”命令添加到披萨中但是,代码没有将其添加到Pizza的TopingDecorator ArrayList中应该是下面这样的(我正在尝试将意大利腊肠和苏打酱添加到AmericanPan比萨饼中(这扩展了普通比萨饼的等级)):

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

这是我的披萨课:

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

    }

这是我的美国班:

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

我想在意大利腊肠课上把意大利腊肠加在美国烤盘上:

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

    }

我正试图在主类中的函数中添加以下代码,该函数操作整个过程:

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

共 (2) 个答案

  1. # 1 楼答案

    我想,你的实现是错误的。使用接口、抽象类的装饰器模式

    Here

    您可以看到,Java的正确实现是什么

  2. # 2 楼答案

    您完全搞错了,使用decorator模式,您使用不同的decorator类来创建不同类型的实例。在你的例子中,这意味着你不能在比萨中添加多个配料,因为配料本身就是比萨,所以萨拉米是萨拉米比萨,胡椒是胡椒比萨,而不是两个配料

    如果你想在一个比萨饼上添加多个配料,那么装饰师不是合适的模式

    这是我的简化装饰器实现

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