【发布时间】:2016-11-13 01:40:33
【问题描述】:
我正在尝试从“BuildOrder”类中获取数组“t”、“p”和“s”到另一个类。以下是参考代码:
我尝试创建方法来返回它们并在另一个类中检索它们,但我不知道如何去做。我正在尝试将装饰器模式添加到复合模式的基本代码中 这里的组件是复合模式的接口
package composite;
public class BuildOrder {
public static Component getOrder()
{
Composite order = new Composite( "Order" ) ;
order.addChild(new Leaf("Crispy Onion Strings", 5.50 ));
order.addChild(new Leaf("The Purist", 8.00 ));
//Composite customBurger = new Composite( "Build Your Own Burger" ) ;
String[] t={"Bermuda Red Onion","Black Olives","Carrot Strings","Coleslaw"};
String[] p={"Applewood Smoked Bacon"};
String[] s={"Apricot Sauce"};
/*customBurger.addChild(new Leaf("Beef, 1/3 lb on a Bun",9.50 )); // base price for 1/3 lb
customBurger.addChild(new Leaf("Danish Blue Cheese", 0.00 )); // 1 cheese free, extra cheese +1.00
customBurger.addChild(new Leaf("Horseradish Cheddar", 1.00 )); // extra cheese +1.00
customBurger.addChild(new Leaf("Bermuda Red Onion", 0.00 )); // 4 toppings free, extra +.75
customBurger.addChild(new Leaf("Black Olives", 0.00 )); // 4 toppings free, extra +.75
customBurger.addChild(new Leaf("Carrot Strings", 0.00 )); // 4 toppings free, extra +.75
customBurger.addChild(new Leaf("Coleslaw", 0.00 )); // 4 toppings free, extra +.75
customBurger.addChild(new Leaf("Applewood Smoked Bacon", 1.50 )); // premium topping +1.50
customBurger.addChild(new Leaf("Apricot Sauce", 0.00 )); // 1 sauce free, extra +.75
order.addChild( customBurger );*/
return order ;
}
public String[] gettoppings()
{ return t;
}
public String[] getpremium()
{ return p;
}
public String[] getSauces()
{ return s;
}
}
//following is the class where I want to use the above strings
public class Sauce implements LeafDecorator
{
public String Sauce()
{ BuildOrder bo=new BuildOrder();
String[] order=bo.getSauces(String[] s);
}
}
【问题讨论】:
-
public String gettoppings(String t) { return t; 有什么用? } ?它直接返回输入?
-
@HendrikT 这是我尝试做一个 getter 方法。
标签: java arrays string design-patterns decorator