## 装饰设计模式### 简介装饰设计模式(Decorator Design Pattern)属于结构型模式,允许向一个现有的对象动态地添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在需要的时候,可以透明地为原对象添加新的职责。### 核心思想装饰模式的核心思想是
开闭原则
,即对扩展开放,对修改关闭。通过使用装饰类,可以在不修改原有代码的情况下,为对象添加新的行为。### 类图```+------------+| Component |+------------+| operation()|+------------+^|+------------+| ConcreteComponent|+------------+| operation()|+------------+^|+------------+| Decorator |+------------+| component || operation()|+------------+^ ^| |+------------+ +------------+| DecoratorA | | DecoratorB |+------------+ +------------+| operation()| | operation()|+------------+ +------------+ ```### 角色
Component(组件)
:定义一个对象接口,可以给这个对象动态地添加职责。
ConcreteComponent(具体组件)
:定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator(装饰抽象类)
:维持一个指向 Component 对象的指针,并定义一个与 Component 接口一致的接口。
ConcreteDecorator(具体装饰类)
:负责给构件对象“贴”上具体的装饰。### 代码示例 (Java)```java // 组件接口 interface Coffee {double cost();String description(); }// 具体组件:Espresso class Espresso implements Coffee {@Overridepublic double cost() {return 1.99;}@Overridepublic String description() {return "Espresso";} }// 装饰抽象类 abstract class CoffeeDecorator implements Coffee {protected Coffee coffee;public CoffeeDecorator(Coffee coffee) {this.coffee = coffee;}@Overridepublic double cost() {return coffee.cost();}@Overridepublic String description() {return coffee.description();} }// 具体装饰类:Milk class Milk extends CoffeeDecorator {public Milk(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return super.cost() + 0.5;}@Overridepublic String description() {return super.description() + ", Milk";} }// 具体装饰类:Sugar class Sugar extends CoffeeDecorator {public Sugar(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return super.cost() + 0.2;}@Overridepublic String description() {return super.description() + ", Sugar";} }public class Main {public static void main(String[] args) {Coffee espresso = new Espresso();System.out.println(espresso.description() + ": $" + espresso.cost());Coffee milkCoffee = new Milk(espresso);System.out.println(milkCoffee.description() + ": $" + milkCoffee.cost());Coffee milkSugarCoffee = new Sugar(milkCoffee);System.out.println(milkSugarCoffee.description() + ": $" + milkSugarCoffee.cost());} } ```### 优缺点#### 优点
符合开闭原则。
扩展性强,可以轻松地添加新的装饰类。
灵活性和可定制性高,可以根据需要组合不同的装饰类。#### 缺点
可能会产生很多小类,增加代码复杂度。
过度使用装饰模式可能会使代码难以理解和维护。### 应用场景
当需要给一个对象动态地添加职责,而不想通过继承来修改其结构时。
当需要对一个对象的职责进行扩展,并且可以根据需要自由组合不同的扩展功能时。
当系统需要支持多种不同的功能组合,而不想使用大量的子类来实现时。### 总结装饰设计模式是一种强大且灵活的设计模式,它可以帮助我们编写更易于扩展和维护的代码。但是,也需要注意避免过度使用装饰模式,以免增加代码的复杂度。
装饰设计模式
简介装饰设计模式(Decorator Design Pattern)属于结构型模式,允许向一个现有的对象动态地添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在需要的时候,可以透明地为原对象添加新的职责。
核心思想装饰模式的核心思想是**开闭原则**,即对扩展开放,对修改关闭。通过使用装饰类,可以在不修改原有代码的情况下,为对象添加新的行为。
类图```+------------+| Component |+------------+| operation()|+------------+^|+------------+| ConcreteComponent|+------------+| operation()|+------------+^|+------------+| Decorator |+------------+| component || operation()|+------------+^ ^| |+------------+ +------------+| DecoratorA | | DecoratorB |+------------+ +------------+| operation()| | operation()|+------------+ +------------+ ```
角色* **Component(组件)**:定义一个对象接口,可以给这个对象动态地添加职责。 * **ConcreteComponent(具体组件)**:定义了一个具体的对象,也可以给这个对象添加一些职责。 * **Decorator(装饰抽象类)**:维持一个指向 Component 对象的指针,并定义一个与 Component 接口一致的接口。 * **ConcreteDecorator(具体装饰类)**:负责给构件对象“贴”上具体的装饰。
代码示例 (Java)```java // 组件接口 interface Coffee {double cost();String description(); }// 具体组件:Espresso class Espresso implements Coffee {@Overridepublic double cost() {return 1.99;}@Overridepublic String description() {return "Espresso";} }// 装饰抽象类 abstract class CoffeeDecorator implements Coffee {protected Coffee coffee;public CoffeeDecorator(Coffee coffee) {this.coffee = coffee;}@Overridepublic double cost() {return coffee.cost();}@Overridepublic String description() {return coffee.description();} }// 具体装饰类:Milk class Milk extends CoffeeDecorator {public Milk(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return super.cost() + 0.5;}@Overridepublic String description() {return super.description() + ", Milk";} }// 具体装饰类:Sugar class Sugar extends CoffeeDecorator {public Sugar(Coffee coffee) {super(coffee);}@Overridepublic double cost() {return super.cost() + 0.2;}@Overridepublic String description() {return super.description() + ", Sugar";} }public class Main {public static void main(String[] args) {Coffee espresso = new Espresso();System.out.println(espresso.description() + ": $" + espresso.cost());Coffee milkCoffee = new Milk(espresso);System.out.println(milkCoffee.description() + ": $" + milkCoffee.cost());Coffee milkSugarCoffee = new Sugar(milkCoffee);System.out.println(milkSugarCoffee.description() + ": $" + milkSugarCoffee.cost());} } ```
优缺点
优点* 符合开闭原则。 * 扩展性强,可以轻松地添加新的装饰类。 * 灵活性和可定制性高,可以根据需要组合不同的装饰类。
缺点* 可能会产生很多小类,增加代码复杂度。 * 过度使用装饰模式可能会使代码难以理解和维护。
应用场景* 当需要给一个对象动态地添加职责,而不想通过继承来修改其结构时。 * 当需要对一个对象的职责进行扩展,并且可以根据需要自由组合不同的扩展功能时。 * 当系统需要支持多种不同的功能组合,而不想使用大量的子类来实现时。
总结装饰设计模式是一种强大且灵活的设计模式,它可以帮助我们编写更易于扩展和维护的代码。但是,也需要注意避免过度使用装饰模式,以免增加代码的复杂度。