装饰器设计模式
- 🚩什么是装饰器设计模式?
- 🚩装饰器设计模式的特点
- 🚩装饰器设计模式的结构
- 🚩装饰器设计模式的优缺点
- 🚩装饰器设计模式的Java实现
- 🚩代码总结
- 🚩总结
🚩什么是装饰器设计模式?
装饰器设计模式(Decorator Pattern) 是一种 结构型设计模式,它允许你通过将对象放入 特殊封装对象 中来为原对象 动态添加新的行为。装饰器模式的核心思想是 在不改变原对象结构的情况下,扩展其功能。
使用场景
-
当需要 动态地添加或撤销对象的功能 时。
-
当 继承不适合扩展功能 时(例如,子类数量爆炸或功能组合复杂)。
-
当需要 为对象添加多个独立的功能,且这些功能可以自由组合时。
🚩装饰器设计模式的特点
-
动态扩展:装饰器模式允许在运行时动态地为对象添加功能。
-
灵活性:可以自由组合多个装饰器,实现功能的叠加。
-
避免继承:通过组合替代继承,避免子类数量爆炸。
-
透明性:装饰器对象与原对象具有相同的接口,客户端无需知道是否被装饰。
🚩装饰器设计模式的结构
装饰器模式主要包含以下部分:
-
Component(抽象组件):定义对象的接口,可以是抽象类或接口。
-
ConcreteComponent(具体组件):实现
Component
接口,是被装饰的原始对象。 -
Decorator(抽象装饰器):继承或实现
Component
,并持有一个Component
对象的引用。 -
ConcreteDecorator(具体装饰器):实现
Decorator
,为Component
添加新的行为。
图例:
🚩装饰器设计模式的优缺点
✅ 优点
-
动态扩展:可以在运行时动态地为对象添加功能。
-
灵活性:可以自由组合多个装饰器,实现功能的叠加。
-
避免继承:通过组合替代继承,避免子类数量爆炸。
-
透明性:装饰器对象与原对象具有相同的接口,客户端无需知道是否被装饰。
❌ 缺点
-
复杂性:装饰器模式会增加系统的复杂性,尤其是在多层装饰时。
-
调试困难:由于装饰器是动态添加的,调试时可能难以追踪具体的装饰逻辑。
🚩装饰器设计模式的Java实现
代码地址:GitHub
- 创建
抽象组件 Person
,定义对象的接口。
/*** @author hanson.huang* @version V1.0* @ClassName Person* @Description 抽象组件 Person* @date 2025/3/24 15:35**/
public abstract class Person {protected String name;public abstract void Operation();// 职责
}
- 创建
具体组件 Student
,实现Person
接口。
/*** @author hanson.huang* @version V1.0* @ClassName Student* @Description 具体组件* @date 2025/3/24 15:41**/
public class Student extends Person {public Student(String name) {this.name = name;}@Overridepublic void Operation() {System.out.println(name + "的职责:学习 ");}
}
- 创建
抽象装饰器 Decorator
,继承Person
并持有一个Person 对象的引用
。
/*** @author hanson.huang* @version V1.0* @ClassName Decorator* @Description 抽象装饰器* @date 2025/3/24 15:42**/
public abstract class Decorator extends Person {protected Person person;
}
-
创建 具体装饰器
DecoratorA
和DecoratorB
,分别实现新的行为。- DecoratorA
/*** @author hanson.huang* @version V1.0* @ClassName DecoratorA* @Description 具体装饰器 DecoratorA* @date 2025/3/24 15:42**/ public class DecoratorA extends Decorator {public DecoratorA(Person person) {this.person = person;}@Overridepublic void Operation() {// 职责person.Operation();// 原本的职责System.out.print("写作业 ");} }
- DecoratorB
/*** @author hanson.huang* @version V1.0* @ClassName DecoratorB* @Description 具体装饰器 DecoratorB* @date 2025/3/24 15:45**/ public class DecoratorB extends Decorator {public DecoratorB(Person person) {this.person = person;}@Overridepublic void Operation() {// 职责person.Operation(); // 原本的职责System.out.print("考试 ");} }
-
测试装饰器模式
/*** @author hanson.huang* @version V1.0* @ClassName DecoratorPattern* @Description 测试装饰器模式* @date 2025/3/24 15:46**/
public class DecoratorPattern {public static void main(String[] args) {Person zhangsan = new Student("张三");zhangsan= new DecoratorA(zhangsan);zhangsan= new DecoratorB(zhangsan);zhangsan.Operation();System.out.println("\n=====我是分割线=====");// 对象链Person lisi = new DecoratorB(new DecoratorA(new Student("李四")));lisi.Operation();}
}
📌 运行结果
🚩代码总结
-
抽象组件 Person
定义对象的接口。 -
具体组件 Student
实现Person 接口
,是被装饰的原始对象。 -
抽象装饰器 Decorator
继承Person
并持有一个Person 对象的引用
。 -
具体装饰器 DecoratorA 和 DecoratorB
分别实现新的行为。 -
客户端
通过组合装饰器动态扩展对象的功能。
🚩总结
-
装饰器设计模式(Decorator Pattern) 允许你通过将对象放入 特殊封装对象 中来为原对象 动态添加新的行为。
-
适用于 动态扩展对象功能、避免继承导致的子类数量爆炸 或 自由组合多个功能 的场景。
-
Java 实现 需要 定义抽象组件、具体组件、抽象装饰器 和 具体装饰器,并通过组合方式动态扩展功能。
✅ 适用场景:
-
需要 动态地添加或撤销对象的功能 时。
-
当 继承不适合扩展功能 时。
-
需要 为对象添加多个独立的功能,且这些功能可以自由组合时。
-
Java中许多地方用到了装饰器模式,例如,I/O流中的
InputStream
就是抽象的基础组件,而,BufferedInputStream
和DataInputStream
属于是装饰器类。Java的Collections 集合类
中Collections.synchronizedList
方法返回的列表是装饰后的组件,它具备了线程安全的特性
。