装饰器模式
什么是装饰器模式?
装饰器模式是一种结构型设计模式,它允许我们在不改变原有对象结构的情况下,动态地给对象添加新的功能。简单来说,就像给一个礼物包装一样,我们可以在不改变礼物本身的情况下,给它加上漂亮的包装纸、丝带、卡片等装饰。
核心思想
- 不修改原有代码:遵循开闭原则,对扩展开放,对修改关闭
- 动态组合:在运行时动态地组合不同的功能
- 透明性:装饰后的对象与原始对象具有相同的接口
结构组成
Component(组件接口)
├── ConcreteComponent(具体组件)
└── Decorator(装饰器抽象类)
├── ConcreteDecoratorA(具体装饰器A)
└── ConcreteDecoratorB(具体装饰器B)
示例一:咖啡店点餐系统
想象一个咖啡店的点餐系统,基础咖啡可以添加各种配料(牛奶、糖、摩卡等)。
基础接口和类
// 咖啡接口
public interface Coffee {
String getDescription();
double getCost();
}
// 基础咖啡
public class SimpleCoffee implements Coffee {
@Override
public String getDescription() {
return "简单咖啡";
}
@Override
public double getCost() {
return 10.0;
}
}
装饰器抽象类
// 咖啡装饰器抽象类
public abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
@Override
public String getDescription() {
return coffee.getDescription();
}
@Override
public double getCost() {
return coffee.getCost();
}
}
具体装饰器
// 牛奶装饰器
public class MilkDecorator extends CoffeeDecorator {
public MilkDecorator(Coffee coffee) {
super(coffee);
}
@Override
public String getDescription() {
return coffee.getDescription() + " + 牛奶";
}
@Override
public double getCost() {
return coffee.getCost() + 2.0;
}
}
// 糖装饰器
public class SugarDecorator extends CoffeeDecorator {
public SugarDecorator(Coffee coffee) {
super(coffee);
}
@Override
public String getDescription() {
return coffee.getDescription() + " + 糖";
}
@Override
public double getCost() {
return coffee.getCost() + 1.0;
}
}
// 摩卡装饰器
public class MochaDecorator extends CoffeeDecorator {
public MochaDecorator(Coffee coffee) {
super(coffee);
}
@Override
public String getDescription() {
return coffee.getDescription() + " + 摩卡";
}
@Override
public double getCost() {
return coffee.getCost() + 3.0;
}
}
使用示例
public class CoffeeShop {
public static void main(String[] args) {
// 基础咖啡
Coffee coffee = new SimpleCoffee();
System.out.println("订单: " + coffee.getDescription());
System.out.println("价格: ¥" + coffee.getCost());
// 加牛奶的咖啡
Coffee milkCoffee = new MilkDecorator(coffee);
System.out.println("订单: " + milkCoffee.getDescription());
System.out.println("价格: ¥" + milkCoffee.getCost());
// 加牛奶和糖的咖啡
Coffee sweetMilkCoffee = new SugarDecorator(milkCoffee);
System.out.println("订单: " + sweetMilkCoffee.getDescription());
System.out.println("价格: ¥" + sweetMilkCoffee.getCost());
// 豪华版:加牛奶、糖和摩卡的咖啡
Coffee luxuryCoffee = new MochaDecorator(sweetMilkCoffee);
System.out.println("订单: " + luxuryCoffee.getDescription());
System.out.println("价格: ¥" + luxuryCoffee.getCost());
}
}
输出结果:
订单: 简单咖啡
价格: ¥10.0
订单: 简单咖啡 + 牛奶
价格: ¥12.0
订单: 简单咖啡 + 牛奶 + 糖
价格: ¥13.0
订单: 简单咖啡 + 牛奶 + 糖 + 摩卡
价格: ¥16.0
示例二:文件读写系统
这个示例展示如何为文件操作添加加密、压缩等功能。
基础接口
// 数据源接口
public interface DataSource {
void writeData(String data);
String readData();
}
// 文件数据源
public class FileDataSource implements DataSource {
private String filename;
public FileDataSource(String filename) {
this.filename = filename;
}
@Override
public void writeData(String data) {
System.out.println("写入文件 " + filename + ": " + data);
}
@Override
public String readData() {
return "从文件 " + filename + " 读取的数据";
}
}
装饰器抽象类
// 数据源装饰器
public abstract class DataSourceDecorator implements DataSource {
protected DataSource dataSource;
public DataSourceDecorator(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void writeData(String data) {
dataSource.writeData(data);
}
@Override
public String readData() {
return dataSource.readData();
}
}
具体装饰器
// 加密装饰器
public class EncryptionDecorator extends DataSourceDecorator {
public EncryptionDecorator(DataSource dataSource) {
super(dataSource);
}
@Override
public void writeData(String data) {
String encryptedData = encrypt(data);
super.writeData(encryptedData);
}
@Override
public String readData() {
String encryptedData = super.readData();
return decrypt(encryptedData);
}
private String encrypt(String data) {
return "加密(" + data + ")";
}
private String decrypt(String data) {
return data.replace("加密(", "").replace(")", "");
}
}
// 压缩装饰器
public class CompressionDecorator extends DataSourceDecorator {
public CompressionDecorator(DataSource dataSource) {
super(dataSource);
}
@Override
public void writeData(String data) {
String compressedData = compress(data);
super.writeData(compressedData);
}
@Override
public String readData() {
String compressedData = super.readData();
return decompress(compressedData);
}
private String compress(String data) {
return "压缩(" + data + ")";
}
private String decompress(String data) {
return data.replace("压缩(", "").replace(")", "");
}
}
使用示例
public class FileSystemDemo {
public static void main(String[] args) {
// 基础文件操作
DataSource file = new FileDataSource("test.txt");
file.writeData("Hello World");
System.out.println(file.readData());
System.out.println("---");
// 加密文件操作
DataSource encryptedFile = new EncryptionDecorator(file);
encryptedFile.writeData("Hello World");
System.out.println(encryptedFile.readData());
System.out.println("---");
// 压缩文件操作
DataSource compressedFile = new CompressionDecorator(file);
compressedFile.writeData("Hello World");
System.out.println(compressedFile.readData());
System.out.println("---");
// 加密且压缩的文件操作
DataSource secureFile = new CompressionDecorator(new EncryptionDecorator(file));
secureFile.writeData("Hello World");
System.out.println(secureFile.readData());
}
}
输出结果:
写入文件 test.txt: Hello World
从文件 test.txt 读取的数据
---
写入文件 test.txt: 加密(Hello World)
Hello World
---
写入文件 test.txt: 压缩(Hello World)
Hello World
---
写入文件 test.txt: 压缩(加密(Hello World))
Hello World
装饰器模式的优点
- 灵活性高:可以动态地组合不同的功能
- 符合开闭原则:新增功能不需要修改现有代码
- 单一职责:每个装饰器只负责一个功能
- 易于扩展:添加新的装饰器很简单
装饰器模式的缺点
- 类数量增加:每个装饰器都需要一个类
- 调试困难:多层装饰可能导致调试复杂
- 性能开销:多层装饰可能影响性能
适用场景
- 需要动态地给对象添加功能
- 不想使用继承来扩展功能
- 需要组合多种功能
- 希望保持类的单一职责
总结
装饰器模式就像给礼物包装一样,我们可以在不改变原有对象的情况下,动态地给它添加新的功能。这种模式特别适合需要灵活组合多种功能的场景,比如咖啡店的配料系统、文件操作的加密压缩等。通过装饰器模式,我们可以轻松地实现功能的组合,同时保持代码的清晰和可维护性。
