装饰者模式
概念
装饰者模式(Decorator Pattern)是一种结构型设计模式,允许向一个对象动态地添加额外的职责,而不影响其他对象。与继承相比,装饰者模式更加灵活,可以在运行时为对象添加新的功能。
应用场景
- 动态地扩展对象的功能:当你需要在运行时动态添加对象功能,而不修改原始类或通过继承增加子类时,可以使用装饰者模式。例如为用户界面组件动态添加滚动条、边框等功能。
- 避免类爆炸:如果使用继承来扩展功能会导致大量子类的增加,装饰者模式可以通过对象组合减少子类的数量。
- 扩展对象功能的灵活性:你可以根据需要选择装饰对象,以组合不同的功能,达到灵活扩展的目的。
注意点
- 注意装饰链的顺序:装饰者可以嵌套调用,但装饰顺序会影响最终效果。
- 装饰者与被装饰者接口一致:确保装饰者和被装饰的对象实现相同的接口,以便可以相互替换。
- 运行时灵活性:装饰者模式比静态继承更具灵活性,但可能增加系统的复杂性,因为需要动态组合多个装饰对象。
核心要素
- Component(抽象构件):定义被装饰对象的接口,装饰者和具体构件都要实现这个接口。
- ConcreteComponent(具体构件):实现抽象构件接口,表示被装饰的具体对象。
- Decorator(装饰者抽象类):持有一个构件对象的引用,并定义与抽象构件相同的接口,用来装饰具体对象。
- ConcreteDecorator(具体装饰者):扩展装饰者,增加额外的功能。
Java代码完整示例
// 抽象构件
interface Component {
void operation();
}
// 具体构件
class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("具体构件的操作");
}
}
// 抽象装饰者
abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation(); // 委托给具体构件
}
}
// 具体装饰者A
class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedBehaviorA();
}
private void addedBehaviorA() {
System.out.println("具体装饰者A的额外行为");
}
}
// 具体装饰者B
class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedBehaviorB();
}
private void addedBehaviorB() {
System.out.println("具体装饰者B的额外行为");
}
}
// 客户端
public class Client {
public static void main(String[] args) {
Component component = new ConcreteComponent();
// 使用装饰者A装饰具体构件
Component decoratorA = new ConcreteDecoratorA(component);
decoratorA.operation();
System.out.println("-----");
// 使用装饰者B装饰具体构件
Component decoratorB = new ConcreteDecoratorB(component);
decoratorB.operation();
System.out.println("-----");
// 使用装饰者A和B嵌套装饰
Component decoratorAB = new ConcreteDecoratorA(new ConcreteDecoratorB(component));
decoratorAB.operation();
}
}
输出结果:
具体构件的操作
具体装饰者A的额外行为
-----
具体构件的操作
具体装饰者B的额外行为
-----
具体构件的操作
具体装饰者B的额外行为
具体装饰者A的额外行为
各种变形用法完整示例
-
动态添加多个装饰功能
可以将装饰者模式嵌套使用,动态添加多个装饰功能。代码示例:
public class ClientMultipleDecorators { public static void main(String[] args) { Component component = new ConcreteComponent(); // 动态添加多个装饰功能 Component decoratorA = new ConcreteDecoratorA(component); Component decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB.operation(); // 先执行A的功能,再执行B的功能 } }
-
透明装饰者模式
装饰者和具体组件具有相同接口,客户端无需关心对象是否被装饰。代码示例:
// 客户端透明调用 public class ClientTransparentDecorator { public static void main(String[] args) { Component component = new ConcreteComponent(); Component decorator = new ConcreteDecoratorA(new ConcreteDecoratorB(component)); // 客户端透明调用,无需关心对象是否被装饰 decorator.operation(); } }
-
装饰者模式与职责链模式结合
将装饰者模式与职责链模式结合,可以让多个装饰者顺序处理请求。代码示例:
// 抽象构件 interface Component { void operation(); } // 具体构件 class ConcreteComponent implements Component { @Override public void operation() { System.out.println("具体构件的操作"); } } // 抽象装饰者 abstract class Decorator implements Component { protected Component component; public Decorator(Component component) { this.component = component; } @Override public void operation() { if (component != null) { component.operation(); } } } // 具体装饰者A class ConcreteDecoratorA extends Decorator { public ConcreteDecoratorA(Component component) { super(component); } @Override public void operation() { super.operation(); System.out.println("装饰者A处理"); } } // 具体装饰者B class ConcreteDecoratorB extends Decorator { public ConcreteDecoratorB(Component component) { super(component); } @Override public void operation() { super.operation(); System.out.println("装饰者B处理"); } } // 客户端 public class ClientDecoratorChain { public static void main(String[] args) { Component component = new ConcreteComponent(); Component decoratorA = new ConcreteDecoratorA(component); Component decoratorB = new ConcreteDecoratorB(decoratorA); decoratorB.operation(); // 顺序处理请求 } }
-
使用Java I/O类库中的装饰者模式
Java标准库中的I/O类库广泛使用了装饰者模式。例如BufferedInputStream
和FileInputStream
之间的关系就是装饰者模式的应用。代码示例:
import java.io.*; public class IOExample { public static void main(String[] args) { try { // FileInputStream 是具体构件,BufferedInputStream 是装饰者 InputStream inputStream = new BufferedInputStream(new FileInputStream("test.txt")); int data = inputStream.read(); while (data != -1) { System.out.print((char) data); data = inputStream.read(); } inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
通过装饰者模式,可以在不修改对象的前提下,动态地给对象添加新的功能,并且支持多层装饰。该模式的灵活性和可扩展性,使得它在许多场景下非常实用。