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

装饰者模式在Java中是一种结构型设计模式,用来动态地给对象添加新的功能,而不改变其原有结构。它通过组合的方式,在不使用继承的前提下扩展对象的行为,比继承更灵活。
要实现装饰者模式,需要明确四个核心角色:
Java标准库中的java.io包是装饰者模式的典型应用。以InputStream为例:
示例代码:
立即学习“Java免费学习笔记(深入)”;
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
使用装饰者模式时注意以下几点:
以上就是Java中装饰者模式使用方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号