请编写一个装饰器模式的实际应用案例代码,并解释其工作原理。
让我们通过一个简单的在咖啡店的例子来实现装饰器模式。我们将设计一个系统,可以用来计算咖啡订单的费用。我们有几种类型的咖啡,如Espresso,HouseBlend等,还有一些调料,如Milk,Whip等。我们希望客户可以随意选择咖啡和调料的组合,并能正确地计算出订单的总费用。
首先,我们定义一个抽象的Beverage
类:
public abstract class Beverage {
String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}
然后,我们定义一些具体的咖啡类,这些类都继承自Beverage
:
public class Espresso extends Beverage {
public Espresso() {
description = "Espresso";
}
public double cost() {
return 1.99;
}
}
public class HouseBlend extends Beverage {
public HouseBlend() {
description = "House Blend Coffee";
}
public double cost() {
return 0.89;
}
}
接下来,我们定义一个CondimentDecorator
类,它也是继承自Beverage
:
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}
然后我们可以定义一些具体的调料类,这些类都继承自CondimentDecorator
:
public class Milk extends CondimentDecorator {
Beverage beverage;
public Milk(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Milk";
}
public double cost() {
return 0.10 + beverage.cost();
}
}
public class Whip extends CondimentDecorator {
Beverage beverage;
public Whip(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Whip";
}
public double cost() {
return 0.20 + beverage.cost();
}
}
最后,我们可以在客户端代码中创建咖啡和调料的组合:
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " " + beverage.cost());
Beverage beverage2 = new HouseBlend();
beverage2 = new Milk(beverage2);
beverage2 = new Whip(beverage2);
System.out.println(beverage2.getDescription() + "" + beverage2.cost());
这个例子展示了装饰器模式的强大之处:我们可以动态地、任意组合地添加新的行为(这里是调料)到对象(这里是咖啡)上,而不需要修改这些对象的代码。