题目:设计一个简单的咖啡店系统,需要支持下单、制作咖啡、送餐等功能。其中,咖啡分为浓缩咖啡、美式咖啡和拿铁咖啡,每种咖啡可添加不同口味的糖浆、牛奶和奶泡。咖啡的成本根据咖啡类型和添加的配料不同而有所区别。请使用面向对象的思想,并使用适当的设计模式来实现该咖啡店系统。请提供类的设计和相关的代码实现。 (要求先说明采用了那些模式,再设计代码)
时间: 2024-03-08 09:48:08 浏览: 186
使用面向对象的思想定义系统数据(会员和商品信息)、实现显示客户信息、添加客户和幸运大放送功能,并定义会员类,商品类进行数组存储
该系统可以采用以下设计模式:
1. 工厂模式:用于创建咖啡和配料对象,将创建对象的过程与客户端分离,提高代码的可维护性和可扩展性。
2. 装饰器模式:用于动态地添加配料,同时保持咖啡的原始接口不变,提高代码的可复用性和可扩展性。
3. 策略模式:用于计算咖啡的成本,根据不同的咖啡类型和添加的配料使用不同的计算策略,提高代码的可扩展性和可维护性。
下面是类的设计和相关代码实现:
Coffee(抽象咖啡类)
```java
public abstract class Coffee {
protected String description;
public String getDescription() {
return description;
}
public abstract double cost();
}
```
Espresso(浓缩咖啡类)
```java
public class Espresso extends Coffee {
public Espresso() {
description = "Espresso";
}
public double cost() {
return 1.99;
}
}
```
Americano(美式咖啡类)
```java
public class Americano extends Coffee {
public Americano() {
description = "Americano";
}
public double cost() {
return 2.49;
}
}
```
Latte(拿铁咖啡类)
```java
public class Latte extends Coffee {
public Latte() {
description = "Latte";
}
public double cost() {
return 3.29;
}
}
```
CondimentDecorator(抽象装饰器类)
```java
public abstract class CondimentDecorator extends Coffee {
public abstract String getDescription();
}
```
Milk(牛奶装饰器类)
```java
public class Milk extends CondimentDecorator {
Coffee coffee;
public Milk(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Milk";
}
public double cost() {
return coffee.cost() + 0.25;
}
}
```
Whip(奶泡装饰器类)
```java
public class Whip extends CondimentDecorator {
Coffee coffee;
public Whip(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Whip";
}
public double cost() {
return coffee.cost() + 0.35;
}
}
```
Syrup(糖浆装饰器类)
```java
public class Syrup extends CondimentDecorator {
Coffee coffee;
public Syrup(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription() + ", Syrup";
}
public double cost() {
return coffee.cost() + 0.20;
}
}
```
CoffeeFactory(咖啡工厂类)
```java
public class CoffeeFactory {
public Coffee createCoffee(String type) {
Coffee coffee = null;
if (type.equals("Espresso")) {
coffee = new Espresso();
} else if (type.equals("Americano")) {
coffee = new Americano();
} else if (type.equals("Latte")) {
coffee = new Latte();
}
return coffee;
}
}
```
CoffeeCostCalculator(咖啡成本计算类)
```java
public class CoffeeCostCalculator {
private Coffee coffee;
public CoffeeCostCalculator(Coffee coffee) {
this.coffee = coffee;
}
public double calculateCost() {
return coffee.cost();
}
}
```
使用示例:
```java
CoffeeFactory coffeeFactory = new CoffeeFactory();
Coffee espresso = coffeeFactory.createCoffee("Espresso");
espresso = new Milk(espresso);
espresso = new Syrup(espresso);
CoffeeCostCalculator costCalculator = new CoffeeCostCalculator(espresso);
System.out.println("Description: " + espresso.getDescription() + ", Cost: " + costCalculator.calculateCost());
```
输出结果:
```
Description: Espresso, Milk, Syrup, Cost: 2.0
```
阅读全文