2017年上半年软件设计师试题-05
Posted yszd
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017年上半年软件设计师试题-05相关的知识,希望对你有一定的参考价值。
某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种类可能不同,但其制作过程相同。
前台服务员(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 abstract void buildParts(); } 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) {
/*设置构建器*/ this.pizzaBuilder = pizzaBuilder; } public Pizza getPizza(){ return pizzaBuilder.getPizza(); } public void construct() {
/*构建*/ pizzaBuilder.createNewPizza(); pizzaBuilder.buildParts(); } } Class FastFoodOrdering { public static viod mainSting[]args) { Waiter waiter = new Waiter(); PizzaBuilder hawaiian_pizzabuilder = new HawaiianPizzaBuilder(); waiter.setPizzaBuilder(hawaiian_pizzabuilder); waiter.construct(); System.out.println("pizza: " + waiter.getPizza()); } } 程序的输出结果为: Pizza:cross + mild + ham&pineapple