装饰者模式通过组合动态扩展对象功能,核心角色包括抽象组件、具体组件、装饰器和具体装饰器。Java IO中的InputStream体系是典型应用,如BufferedInputStream增强FileInputStream。自定义示例中,Coffee接口被Milk和Sugar逐层装饰,实现描述与价格叠加。该模式要求装饰器与组件接口一致,支持透明嵌套,适用于需灵活扩展功能且避免类爆炸的场景,但应控制嵌套层次以防维护困难。

装饰者模式在Java中是一种结构型设计模式,用来动态地给对象添加新的功能,而不改变其原有结构。它通过组合的方式,在不使用继承的前提下扩展对象的行为,比继承更灵活。
1. 装饰者模式的核心角色
要实现装饰者模式,需要明确四个核心角色:
- Component(抽象组件):定义对象接口,可以是接口或抽象类,比如InputStream。
- ConcreteComponent(具体组件):实现抽象组件的类,是被装饰的对象,比如FileInputStream。
- Decorator(装饰器):持有Component对象的引用,通常也实现Component接口,并在其方法中调用被装饰对象的方法。
- ConcreteDecorator(具体装饰器):在Decorator基础上添加新功能,比如缓冲、加密等。
2. Java IO中的经典应用
Java标准库中的java.io包是装饰者模式的典型应用。以InputStream为例:
- 抽象组件:InputStream
- 具体组件:FileInputStream
- 装饰器:FilterInputStream
- 具体装饰器:BufferedInputStream、DataInputStream
示例代码:
立即学习“Java免费学习笔记(深入)”;
模板采用响应式设计,自动适应手机,电脑及平板显示;满足单一店铺外卖需求。功能:1.菜单分类管理2.菜品管理:菜品增加,删除,修改3.订单管理4.友情链接管理5.数据库备份6.文章模块:如:促销活动,帮助中心7.单页模块:如:企业信息,关于我们更强大的功能在开发中……安装方法:上传到网站根目录,运行http://www.***.com/install 自动
InputStream fis = new FileInputStream("data.txt");
InputStream bis = new BufferedInputStream(fis); // 添加缓冲功能
DataInputStream dis = new DataInputStream(bis); // 可读取基本数据类型
每一层都基于前一个对象进行功能增强,而不是创建新的子类。
3. 自定义装饰者模式示例
假设我们有一个咖啡系统,基础咖啡可以动态添加糖、牛奶等配料。
定义抽象组件:
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
4. 使用要点与注意事项
使用装饰者模式时注意以下几点:
- 装饰器和被装饰对象应实现相同的接口,保证透明性。
- 装饰器内部持有组件对象,通过构造函数传入,支持多层嵌套。
- 可以在调用前后增加逻辑,如日志、权限、缓存等。
- 避免过度嵌套,否则调试困难。
- 适合频繁扩展功能但类数量爆炸的场景。









