装饰模式
The Decorator Pattern is another fundamental design pattern used in software development, especially useful for adding new functionalities to objects dynamically. It provides a flexible alternative to subclassing for extending functionality.
装饰者模式(Decorator Pattern)是软件开发中使用的另一种基本设计模式,尤其适用于为对象动态添加新功能。它为扩展功能提供了一种灵活的子类化替代方案。
Scenario: Coffee Shop Application
Problem Description:
In a coffee shop, the cost of a drink depends on the drink itself (like espresso, latte) and the added ingredients (like milk, soy, mocha). If we were to use inheritance to create every combination, it would quickly become unmanageable. For instance, we would have classes like EspressoWithMilk, LatteWithSoyAndMocha, etc.
在咖啡店,饮料的价格取决于饮料本身(如浓缩咖啡、拿铁咖啡)和添加的配料(如牛奶、豆浆、摩卡咖啡)。如果我们使用继承来创建每一种组合,很快就会变得难以管理。例如,我们会有 EspressoWithMilk 、 LatteWithSoyAndMocha 等类。
How Decorator Pattern Solves the Problem:
Component and Concrete Components:
- The Component in this scenario is a
Beverageclass. 此方案中的组件是一个Beverage类(饮料)。 - Concrete Components are different types of beverages, e.g.,
Espresso,Latte, etc. 具体组件是指不同类型的饮料,如Espresso、Latte等。
- The Component in this scenario is a
Decorator:
- We create an abstract
Decoratorclass that extendsBeverage, which will also have subclasses likeMilk,Soy,Mocharepresenting additional ingredients. 我们创建了一个扩展Beverage的抽象装饰器类,该类还将包含Milk,Soy,Mocha等子类,代表其他成分。
- We create an abstract
Adding New Functionalities:
- Each of these subclasses wraps a
Beverageobject and adds its own behavior (like additional cost for milk or soy) to the existing behavior of theBeverage. 每个子类都封装了一个Beverage对象,并在Beverage的现有行为中添加了自己的行为(如牛奶或大豆的额外成本)。
- Each of these subclasses wraps a
Flexibility:
- This pattern allows adding new ingredients without altering the existing codebase of beverages or other ingredients. 这种模式可以在不改变现有饮料或其他配料代码的情况下添加新配料。
Sample Code
Component
public abstract class Beverage {
String description = "Unknown Beverage";
public String getDescription() {
return description;
}
public abstract double cost();
}Concrete Components
public class Espresso extends Beverage {
public Espresso() {
description = "Espresso";
}
public double cost() {
return 1.99;
}
}
public class Latte extends Beverage {
public Latte() {
description = "Latte";
}
public double cost() {
return 2.49;
}
}Decorator
public abstract class CondimentDecorator extends Beverage {
public abstract String getDescription();
}Concrete Decorators
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 .10 + beverage.cost();
}
}
public class Mocha extends CondimentDecorator {
Beverage beverage;
public Mocha(Beverage beverage) {
this.beverage = beverage;
}
public String getDescription() {
return beverage.getDescription() + ", Mocha";
}
public double cost() {
return .20 + beverage.cost();
}
}Using the Decorator Pattern
public class CoffeeShop {
public static void main(String[] args) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " $" + beverage.cost());
Beverage anotherBeverage = new Latte();
anotherBeverage = new Milk(anotherBeverage);
anotherBeverage = new Mocha(anotherBeverage);
System.out.println(anotherBeverage.getDescription() + " $" + anotherBeverage.cost());
}
}In this example, we dynamically add functionalities (Milk and Mocha) to the Latte object. This approach is much more flexible than a static inheritance hierarchy, as it allows mixing and matching decorations as needed.
在本例中,我们动态地将功能(Milk 和 Mocha)添加到 Latte 对象中。这种方法比静态继承层次结构灵活得多,因为它允许根据需要混合和匹配装饰。

Create By GPT-4
评论已关闭