我必须实施比萨(美国和那不勒斯)装饰图案,其中包括4种不同的浇头(萨拉米香肠,Soudjouk,洋葱,胡椒),其中包括“TopingDecorator”类,其中3种将通过“添加比萨”命令添加到比萨饼中。但是,代码不会将它添加到Pizza的TopingDecorator ArrayList。它应该类似于下面(我正在尝试将Salami和Soudjouk添加到AmericanPan pizza(它扩展了PlainPizza类)):
AmericanPan a = new American();
Salami s = new Salami(a);
Soudjouk so = new Soudjouk(s);
这是我的PlainPizza课程:
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课程:
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课程中添加Salami on American Pan:
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);
答案 0 :(得分:1)
你正在解决这个问题,装饰模式使用不同的装饰器类来创建不同类型的实例。在你的情况下,这意味着你不能在比萨饼上添加多个配料,因为配料本身就是比萨饼,所以萨拉米香肠是萨拉米香肠披萨,辣椒是胡椒披萨而不是两个浇头
如果您想在一个比萨饼中添加多个浇头,那么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());
}
答案 1 :(得分:0)