您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Java的装饰者模式是什么
## 一、设计模式与装饰者模式概述
### 1.1 设计模式简介
设计模式是软件设计中常见问题的典型解决方案,它们是被反复使用、多数人知晓的代码设计经验的总结。在面向对象编程中,设计模式主要分为三大类:
- **创建型模式**(如工厂模式、单例模式)
- **结构型模式**(如装饰者模式、适配器模式)
- **行为型模式**(如观察者模式、策略模式)
### 1.2 装饰者模式的定义
装饰者模式(Decorator Pattern)是一种**结构型设计模式**,它允许向一个现有的对象添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
```java
// 简单示例结构
public interface Component {
void operation();
}
public class ConcreteComponent implements Component {
public void operation() {
System.out.println("基本操作");
}
}
public abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
public void operation() {
component.operation();
}
}
┌─────────────┐ ┌─────────────────┐
│ Component │<------│ Decorator │
└─────────────┘ └─────────────────┘
^ ^
| |
┌─────────────┐ ┌─────────────────┐
│ConcreteComponent│ │ConcreteDecoratorA│
└─────────────┘ └─────────────────┘
Component(抽象组件)
ConcreteComponent(具体组件)
Decorator(抽象装饰类)
ConcreteDecorator(具体装饰类)
// 抽象组件
public interface Coffee {
double getCost();
String getDescription();
}
// 具体组件
public class SimpleCoffee implements Coffee {
public double getCost() {
return 1.0;
}
public String getDescription() {
return "普通咖啡";
}
}
// 抽象装饰类
public abstract class CoffeeDecorator implements Coffee {
protected final Coffee decoratedCoffee;
public CoffeeDecorator(Coffee coffee) {
this.decoratedCoffee = coffee;
}
public double getCost() {
return decoratedCoffee.getCost();
}
public String getDescription() {
return decoratedCoffee.getDescription();
}
}
// 具体装饰类-牛奶
public class WithMilk extends CoffeeDecorator {
public WithMilk(Coffee coffee) {
super(coffee);
}
public double getCost() {
return super.getCost() + 0.5;
}
public String getDescription() {
return super.getDescription() + ", 加牛奶";
}
}
// 具体装饰类-糖
public class WithSugar extends CoffeeDecorator {
public WithSugar(Coffee coffee) {
super(coffee);
}
public double getCost() {
return super.getCost() + 0.2;
}
public String getDescription() {
return super.getDescription() + ", 加糖";
}
}
// 客户端使用
public class Main {
public static void main(String[] args) {
Coffee coffee = new SimpleCoffee();
coffee = new WithMilk(coffee);
coffee = new WithSugar(coffee);
System.out.println("费用: $" + coffee.getCost());
System.out.println("描述: " + coffee.getDescription());
}
}
Java的IO流是装饰者模式的经典应用:
// 基础组件
InputStream inputStream = new FileInputStream("test.txt");
// 装饰者
BufferedInputStream buffered = new BufferedInputStream(inputStream);
DataInputStream data = new DataInputStream(buffered);
特性 | 装饰者模式 | 继承 |
---|---|---|
扩展方式 | 动态组合 | 静态编译时确定 |
灵活性 | 更高(运行时改变) | 较低(编译时固定) |
代码复杂度 | 需要设计抽象装饰层 | 简单直接 |
子类数量 | 按需组合,避免爆炸 | 容易导致子类数量膨胀 |
// 不推荐的深层装饰
Reader reader = new BufferedReader(
new LineNumberReader(
new InputStreamReader(
new FileInputStream("file.txt"))));
当需要暴露装饰者新增的方法时:
public class ScrollableWindow extends WindowDecorator {
public void scrollTo(int x, int y) {
// 新增方法
}
}
// 使用时需要具体类型
ScrollableWindow scrolledWindow = new ScrollableWindow(window);
装饰者模式通过动态组合代替静态继承,提供了更灵活的功能扩展方式。它在Java标准库(如IO流、Swing组件)中有广泛应用,特别适合以下场景: - 需要不影响其他对象的情况下动态添加职责 - 需要可以撤销的职责 - 通过继承扩展不切实际时
正确使用装饰者模式可以使系统保持良好的扩展性,同时避免继承带来的静态特性和子类爆炸问题。
“装饰者模式就像给礼物添加包装纸 - 你可以不断添加新的包装层,而不改变礼物本身。” - 《Head First设计模式》 “`
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。