装饰器模式 Decorator Pattern
装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许通过动态地添加功能来扩展对象的行为,而不需要修改原有的类。
这种模式通常用于增强对象的功能,与继承相比,使用装饰器可以避免类的爆炸增长,因为它允许在运行时动态添加功能,而不必创建多个子类。
- 装饰器模式的组成部分
- 组件接口(Component):定义一个接口,所有具体组件和装饰器都需要实现这个接口。
- 具体组件(Concrete Component):实现了组件接口的基本对象,可以在其上添加新的行为。
- 装饰器(Decorator):实现组件接口,并持有一个组件接口的引用。装饰器的职责是通过扩展组件的功能来增强或覆盖其行为。
- 具体装饰器(Concrete Decorator):实现装饰器,提供新的行为或附加责任。
- 使用场景
- 需要动态地追加对象的功能,而不需要修改其代码。
- 需要在不影响其他对象的情况下,为对象添加职责(功能)。
- 需要避免创建大规模的子类来实现对象的不同组合。
- 例子
-
定义组件接口
定义一个组件接口,用于描述我们要装饰的对象。// Coffee接口 public interface Coffee {String getDescription();double cost(); }
-
创建具体组件
接下来,实现一个具体组件,代表美式咖啡// 美式咖啡 public class AmericanCoffee implements Coffee{@Override public String getDescription() { return "美式咖啡"; } @Override public double cost() { return 10; // 基础价格 } }
-
创建装饰器抽象类
创建一个装饰器抽象类,它实现了咖啡接口,并持有一个咖啡的引用public abstract class CoffeeDecorator implements Coffee {// 持有一个具体组件的引用protected Coffee decoratedCoffee;public CoffeeDecorator(Coffee coffee) { this.decoratedCoffee = coffee; } @Override public String getDescription() { return decoratedCoffee.getDescription(); } }
-
创建具体装饰器
实现具体的装饰器,给咖啡添加额外的功能:例如加奶,加糖等额外功能// 具体装饰器:牛奶 public class MilkDecorator extends CoffeeDecorator { public MilkDecorator(Coffee coffee) { super(coffee); } @Override public String getDescription() { return decoratedCoffee.getDescription() + ", 加奶"; } @Override public double cost() { return decoratedCoffee.cost() + 0.5; // 牛奶附加费用 } } // 具体装饰器:糖 public class SugarDecorator extends CoffeeDecorator { public SugarDecorator(Coffee coffee) { super(coffee); } @Override public String getDescription() { return decoratedCoffee.getDescription() + ", 加糖"; } @Override public double cost() { return decoratedCoffee.cost() + 0.2; // 糖的附加费用 } }
-
使用装饰器
在客户端代码中,你可以通过创建基本的咖啡对象,然后使用装饰器来增加功能:public class CoffeeShop { public static void main(String[] args) { // 创建一杯美式 Coffee coffee = new AmericanCoffee(); System.out.println(coffee.getDescription() + " ¥" + coffee.cost()); // 添加牛奶装饰 coffee = new MilkDecorator(coffee); System.out.println(coffee.getDescription() + " ¥" + coffee.cost()); // 添加糖装饰 coffee = new SugarDecorator(coffee); System.out.println(coffee.getDescription() + " ¥" + coffee.cost()); } }
结果就是
美式咖啡 ¥10 美式咖啡, 加奶 ¥1.5 美式咖啡, 加奶, 加糖 ¥1.7
-
这就是装饰器模型,它的优点如下
- 灵活性:可以在运行-时决定需要添加哪些装饰器,实现了对象的可扩展性。
- 透明性:客户端并不需要知道对象的装饰,只需与接口交互。
- 避免类爆炸:不需要为每一种组合功能创建新类,而是通过装饰器组合功能。
装饰器模式是一种结构型设计模式,它通过将额外的行为动态地附加到对象上,增强了对象的功能。它适用于需要扩展功能同时避免类的爆炸性增长的场景。在实际应用中,装饰器模式可以提供灵活的、可扩展的解决方案。