Java装饰器模式
装饰器模式动态地给对象添加额外功能,比继承更灵活。
模式定义
意图:动态给对象添加职责,不改变原有结构。
装饰器 vs 继承
| 特性 | 装饰器 | 继承 |
|---|---|---|
| 扩展方式 | 动态组合 | 静态继承 |
| 扩展时机 | 运行时 | 编译时 |
| 扩展数量 | 无限制 | 类爆炸 |
| 灵活性 | 高 | 低 |
模式结构
基础组件
Java
// 抽象组件
public interface Coffee {
double getCost();
String getDescription();
}
// 具体组件
public class SimpleCoffee implements Coffee {
@Override
public double getCost() {
return 10;
}
@Override
public String getDescription() {
return "简单咖啡";
}
}
装饰器
Java
// 抽象装饰器
public abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public double getCost() {
return coffee.getCost();
}
@Override
public String getDescription() {
return coffee.getDescription();
}
}
// 具体装饰器:牛奶
public class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) {
super(coffee);
}
@Override
public double getCost() {
return super.getCost() + 2;
}
@Override
public String getDescription() {
return super.getDescription() + " +牛奶";
}
}
// 具体装饰器:糖
public class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) {
super(coffee);
}
@Override
public double getCost() {
return super.getCost() + 1;
}
@Override
public String getDescription() {
return super.getDescription() + " +糖";
}
}
多层装饰
Java
Coffee coffee = new SimpleCoffee(); // 10元
coffee = new MilkDecorator(coffee); // 12元
coffee = new SugarDecorator(coffee); // 13元
coffee = new MilkDecorator(coffee); // 15元(再加牛奶)
System.out.println(coffee.getDescription()); // 简单咖啡 +牛奶 +糖 +牛奶
System.out.println(coffee.getCost()); // 15.0
Java I/O装饰器
Java I/O是装饰器模式的经典应用:
Java
// 基础组件:InputStream
InputStream input = new FileInputStream("file.txt");
// 装饰:缓冲
input = new BufferedInputStream(input);
// 装饰:数据
input = new DataInputStream(input);
// 多层装饰链
InputStream in = new BufferedInputStream(
new DataInputStream(
new FileInputStream("file.txt")
)
);
实际应用示例
文本处理
Java
public interface TextProcessor {
String process(String text);
}
public class PlainText implements TextProcessor {
@Override
public String process(String text) {
return text;
}
}
public abstract class TextDecorator implements TextProcessor {
protected TextProcessor processor;
public TextDecorator(TextProcessor processor) {
this.processor = processor;
}
}
public class BoldDecorator extends TextDecorator {
public BoldDecorator(TextProcessor processor) {
super(processor);
}
@Override
public String process(String text) {
return "<b>" + processor.process(text) + "</b>";
}
}
public class ItalicDecorator extends TextDecorator {
public ItalicDecorator(TextProcessor processor) {
super(processor);
}
@Override
public String process(String text) {
return "<i>" + processor.process(text) + "</i>";
}
}
// 使用
TextProcessor processor = new BoldDecorator(
new ItalicDecorator(new PlainText())
);
String result = processor.process("Hello");
// <b><i>Hello</i></b>
装饰器特点
- 装饰器与组件有相同接口
- 装饰器持有组件引用
- 装饰器可在组件方法前后添加功能
- 可无限叠加装饰器
适用场景
- 动态添加/撤销功能
- 多种功能组合
- 避免继承导致的类爆炸
- Java I/O流处理
注意事项
装饰器需实现组件所有方法
装饰器层数过多影响性能
装饰顺序可能影响结果
装饰器不改变组件核心功能
要点总结
- 装饰器动态添加功能,比继承更灵活
- 装饰器实现组件接口,持有组件引用
- 可多层叠加装饰,任意组合功能
- Java I/O是装饰器的经典应用
- 适用于需要动态组合多种功能的场景
📝 发现内容有误?点击此处直接编辑