装饰者模式是一种结构型设计模式(亦称: 装饰器模式、Wrapper、Decorator),允许你在不改变对象自身结构的情况下,动态地给对象添加新的行为或责任。它通过将对象放入包装器中来实现这一目的,每个包装器(装饰者)都实现了与被包装对象相同的接口,并且可以在运行时动态添加或修改功能。
结构
在装饰者模式中,有几个核心角色:
- Component(组件):定义了被装饰对象的接口,可以是抽象类或接口。
- ConcreteComponent(具体组件):实现了Component接口,是被装饰的对象。
- Decorator(装饰者):也实现了Component接口,持有一个Component实例,并且可以通过构造函数或其他方式将被装饰对象包裹起来。Decorator通常是一个抽象类,它的具体子类实现具体的装饰逻辑。
- ConcreteDecorator(具体装饰者):继承自Decorator,实现了具体的装饰行为,可以在被装饰对象的行为前后添加自定义的行为。
以上是一个模拟咖啡店中咖啡的装饰过程,Coffee是一个接口,SimpleCoffee是实现了Coffee接口的具体组件。CoffeeDecorator是装饰者抽象类,MilkDecorator是具体的装饰者,实现了添加牛奶的功能
代码示例
Coffee:组件
public interface Coffee {
double getCost();
String getIngredients();
}
SimpleCoffee:具体组件
public class SimpleCoffee implements Coffee {
public double getCost() {
return 1.0;
}
public String getIngredients() {
return "Coffee";
}
}
CoffeeDecorator:装饰者
public abstract class CoffeeDecorator implements Coffee {
protected final Coffee decoratedCoffee;
public CoffeeDecorator(Coffee decoratedCoffee) {
this.decoratedCoffee = decoratedCoffee;
}
public double getCost() {
return decoratedCoffee.getCost();
}
public String getIngredients() {
return decoratedCoffee.getIngredients();
}
}
MilkDecorator:具体装饰者
public class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee decoratedCoffee) {
super(decoratedCoffee);
}
public double getCost() {
return super.getCost() + 0.5;
}
public String getIngredients() {
return super.getIngredients() + ", Milk";
}
}
运行示例:
public static void main(String[] args) {
// Simple Coffee
Coffee coffee = new SimpleCoffee();
System.out.println("Cost: " + coffee.getCost() + ", Ingredients: " + coffee.getIngredients());
// Coffee with Milk
Coffee coffeeWithMilk = new MilkDecorator(new SimpleCoffee());
System.out.println("Cost: " + coffeeWithMilk.getCost() + ", Ingredients: " + coffeeWithMilk.getIngredients());
}
优点
- 灵活性:可以动态地给对象添加新功能,不需要修改现有代码。
- 可扩展性:可以通过组合装饰器的方式实现不同组合的行为。
- 遵循开闭原则:无需修改现有代码即可添加新功能
缺点
- 可能产生过多小对象:装饰者模式会增加许多具体装饰者类,可能导致类的数量增加,不易管理。
适用场景
- 需要动态地给对象添加功能,而且要求这些功能可以灵活组合的情况。
- 不希望通过继承来扩展功能,因为继承会造成静态的行为,而装饰者模式能够在运行时动态添加行为。