装饰者模式通过组合动态扩展对象功能,核心角色包括抽象组件、具体组件、装饰器和具体装饰器。Java IO中的InputStream体系是典型应用,如BufferedInputStream增强FileInputStream。自定义示例中,Coffee接口被Milk和Sugar逐层装饰,实现描述与价格叠加。该模式要求装饰器与组件接口一致,支持透明嵌套,适用于需灵活扩展功能且避免类爆炸的场景,但应控制嵌套层次以防维护困难。
装饰者模式在Java中是一种结构型设计模式,用来动态地给对象添加新的功能,而不改变其原有结构。它通过组合的方式,在不使用继承的前提下扩展对象的行为,比继承更灵活。
要实现装饰者模式,需要明确四个核心角色:
象接口,可以是接口或抽象类,比如InputStream。Java标准库中的java.io包是装饰者模式的典型应用。以InputStream为例:
示例代码:
InputStream fis = new FileInputStream("data.txt");
InputStream bis = new BufferedInputStream(fis); // 添加缓冲功能
DataInputStream dis = new DataInputStream(bis); // 可读取基本数据类型
每一层都基于前一个对象进行功能增强,而不是创建新的子类。
假设我们有一个咖啡系统,基础咖啡可以动态添加糖、牛奶等配料。
定义抽象组件:
public interface Coffee {
String getDescription();
double cost();
}
具体组件:
public class SimpleCoffee implements Coffee {
public String getDescription() {
return "纯咖啡";
}
public double cost() {
return 5.0;
}
}
装饰器基类:
public abstract class CoffeeDecorator implements Coffee {
protected Coffee coffee;
public CoffeeDecorator(Coffee coffee) {
this.coffee = coffee;
}
public String getDescription() {
return coffee.getDescription();
}
public double cost() {
return coffee.cost();
}
}
具体装饰器:
public class Milk extends CoffeeDecorator {
public Milk(Coffee coffee) {
super(coffee);
}
public String getDescription() {
return coffee.getDescription() + ", 加牛奶";
}
public double cost() {
return coffee.cost() + 2.0;
}
}
public class Sugar extends CoffeeDecorator {
public Sugar(Coffee coffee) {
super(coffee);
}
public String getDescription() {
return coffee.getDescription() + ", 加糖";
}
public double cost() {
return coffee.cost() + 1.0;
}
}
使用方式:
Coffee myCoffee = new SimpleCoffee();
myCoffee = new Milk(myCoffee);
myCoffee = new Sugar(myCoffee);
System.out.println(myCoffee.getDescription()); // 输出:纯咖啡, 加牛奶, 加糖
System.out.println("总价:" + myCoffee.cost()); // 输出:8.0
使用装饰者模式时注意以下几点: