用建造者模式写一个咖啡厅点餐系统,用java给出代码
时间: 2023-09-05 10:04:33 浏览: 215
### 回答1:
在Java中,可以使用建造者模式创建一个CoffeeShopOrder类,它接受一个客户的请求,并且有一个内部类OrderBuilder来构建客户的订单。public class CoffeeShopOrder { private OrderBuilder orderBuilder;
public CoffeeShopOrder(OrderBuilder orderBuilder) {
this.orderBuilder = orderBuilder;
}
public void makeOrder() {
orderBuilder.addDrink();
orderBuilder.addSide();
orderBuilder.addExtras();
}
public static class OrderBuilder {
private String drink;
private String side;
private String extras;
public OrderBuilder() {}
public OrderBuilder addDrink(String drink) {
this.drink = drink;
return this;
}
public OrderBuilder addSide(String side) {
this.side = side;
return this;
}
public OrderBuilder addExtras(String extras) {
this.extras = extras;
return this;
}
public CoffeeShopOrder build() {
return new CoffeeShopOrder(this);
}
}
}
### 回答2:
建造者模式是一种软件设计模式,可以将复杂对象的创建过程与其表示分离。在咖啡厅点餐系统中,我们可以使用建造者模式来创建不同类型的咖啡和添加不同的配料。
首先,我们需要定义一个咖啡类Coffee,具有名称、类型和配料列表等属性。然后,定义一个抽象建造者类CoffeeBuilder,包含创建咖啡、设置类型和添加配料的方法。接下来,为每种具体的咖啡类型(如拿铁、卡布奇诺等)创建具体的建造者类,继承自CoffeeBuilder,并实现相应的方法。
下面是使用Java代码实现咖啡厅点餐系统的示例:
```java
import java.util.ArrayList;
import java.util.List;
// 定义咖啡类
class Coffee {
private String name;
private String type;
private List<String> ingredients;
public Coffee() {
ingredients = new ArrayList<>();
}
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public void addIngredient(String ingredient) {
ingredients.add(ingredient);
}
public void showInfo() {
System.out.println("Coffee Name: " + name);
System.out.println("Coffee Type: " + type);
System.out.println("Ingredients: ");
for (String ingredient : ingredients) {
System.out.println("- " + ingredient);
}
}
}
// 定义抽象建造者类
abstract class CoffeeBuilder {
protected Coffee coffee;
public Coffee getCoffee() {
return coffee;
}
public void createCoffee() {
coffee = new Coffee();
}
public abstract void setType();
public abstract void addIngredients();
}
// 定义具体建造者类 - 拿铁咖啡
class LatteBuilder extends CoffeeBuilder {
public void setType() {
coffee.setType("Latte");
}
public void addIngredients() {
coffee.addIngredient("Espresso");
coffee.addIngredient("Steamed Milk");
coffee.addIngredient("Milk Foam");
}
}
// 定义具体建造者类 - 卡布奇诺咖啡
class CappuccinoBuilder extends CoffeeBuilder {
public void setType() {
coffee.setType("Cappuccino");
}
public void addIngredients() {
coffee.addIngredient("Espresso");
coffee.addIngredient("Steamed Milk");
coffee.addIngredient("Foamed Milk");
coffee.addIngredient("Cocoa Powder");
}
}
// 定义指导者类
class CoffeeDirector {
private CoffeeBuilder coffeeBuilder;
public void setCoffeeBuilder(CoffeeBuilder coffeeBuilder) {
this.coffeeBuilder = coffeeBuilder;
}
public Coffee getCoffee() {
return coffeeBuilder.getCoffee();
}
public void makeCoffee() {
coffeeBuilder.createCoffee();
coffeeBuilder.setType();
coffeeBuilder.addIngredients();
}
}
// 客户端代码
public class Main {
public static void main(String[] args) {
CoffeeDirector director = new CoffeeDirector();
CoffeeBuilder latteBuilder = new LatteBuilder();
CoffeeBuilder cappuccinoBuilder = new CappuccinoBuilder();
// 创建拿铁咖啡
director.setCoffeeBuilder(latteBuilder);
director.makeCoffee();
Coffee latteCoffee = director.getCoffee();
// 创建卡布奇诺咖啡
director.setCoffeeBuilder(cappuccinoBuilder);
director.makeCoffee();
Coffee cappuccinoCoffee = director.getCoffee();
// 展示咖啡信息
latteCoffee.showInfo();
cappuccinoCoffee.showInfo();
}
}
```
以上就是使用建造者模式实现咖啡厅点餐系统的一个示例。客户端可以通过指导者类选择不同的咖啡建造者来创建相应的咖啡对象,并展示咖啡的信息。
阅读全文