某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类

免费题库2022-08-02  13

问题 某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。前台服务员(Waiter)调度厨师制作套餐。现采用生成器(Builder)模式实现制作过程,得到如图6-1所示的类图。【Java代码】class Pizza{private String parts;public void setParts(String parts){this.parts=parts;}public String toString(  ){return this.parts;}}abstract class PizzaBuilder{protected Pizza pizza;public Pizza getPizza(  ){return pizza;}public void createNewPizza(  ){pizza=new Pizza(  );}public(1);}class HawaiianPizzaBuilder extends PizzaBuilder{public void buildParts(  ){pizza.setParts("cross+mild+ham&pineapp1e”};}class SpicyPizzaBuilder extends PizzaBuilder{public void buildParts(  ){pizza.setParts("pan baked+hot+pepperoni&salami");}}class Waiter{private PizzaBuilder pizzaBuilder;public void setPizzaBuilder(PizzaBuilder pizzaBuilder){/*设置构建器*/(2);}public Pizza getPizza(  ){return pizzaBuilder.getPizza(  );}public void construct(  ){/*构建*/pizzaBuilder.createNewPizza(  );(3);}}Class FastFoodOrdering{public static viod mainSting[]args){Waiter waiter=new Waiter(  );PizzaBuilder hawaiian_pizzabuilder=new HawaiianPizzaBuilder(  );(4);(5);System.out.println("pizza:"+waiter.getPizza(  ));}}程序的输出结果为:Pizza:cross+mild+ham&pineapple

选项

答案

解析 (1)abstract void buildParts();
(2)this.pizzaBuilder=pizzaBuilder
(3)pizzaBuilder.buildParts()
(4)waiter.setPizzaBuilder(hawaiian_pizzabuilder)
(5)waiter.construct()
1.看类图,还差一个buildparts方法,再看下面的类也有buildparts方法,知道应该是abstract void buildParts()。
2.这部分填写设置构建器内容,在waiter类里面,定义pizzaBuilder。
3.从类图知道,构建方法应该是buildParts,当前对象是pizzaBuilder。
4.前面定义了对象waiter,新建hawaiian_pizzabuilder类,调用waiter的set方法。
5.调用waiter类中的construct方法,这样可以得到Pizza。
转载请注明原文地址:https://tihaiku.com/congyezige/2410367.html

最新回复(0)