您好,登录后才能下订单哦!
# 如何从Java IO源码看装饰者模式的用法
## 一、装饰者模式基础概念
### 1.1 设计模式简介
装饰者模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
### 1.2 装饰者模式核心思想
- **动态扩展**:运行时动态地给对象添加职责
- **透明装饰**:装饰后的对象与原始对象具有相同的接口
- **组合优于继承**:通过对象组合而非继承实现功能扩展
### 1.3 UML类图解析
```plantuml
@startuml
interface Component {
+ operation()
}
class ConcreteComponent {
+ operation()
}
class Decorator {
- component: Component
+ operation()
}
class ConcreteDecoratorA {
+ operation()
+ addedBehavior()
}
class ConcreteDecoratorB {
+ operation()
+ addedState
}
Component <|.. ConcreteComponent
Component <|.. Decorator
Decorator <|-- ConcreteDecoratorA
Decorator <|-- ConcreteDecoratorB
Decorator o-- Component
@enduml
Java IO库主要分为两大类: 1. 字节流:InputStream/OutputStream 2. 字符流:Reader/Writer
// 基础组件
FileInputStream fis = new FileInputStream("test.txt");
// 装饰组件
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
// 基础组件
FileReader fr = new FileReader("test.txt");
// 装饰组件
BufferedReader br = new BufferedReader(fr);
LineNumberReader lnr = new LineNumberReader(br);
角色类型 | 字节流示例 | 字符流示例 |
---|---|---|
抽象组件 | InputStream | Reader |
具体组件 | FileInputStream | FileReader |
抽象装饰者 | FilterInputStream | FilterReader |
具体装饰者 | BufferedInputStream | BufferedReader |
public class FilterInputStream extends InputStream {
protected volatile InputStream in;
protected FilterInputStream(InputStream in) {
this.in = in;
}
public int read() throws IOException {
return in.read();
}
// 其他方法类似委托
}
public class BufferedInputStream extends FilterInputStream {
private static int DEFAULT_BUFFER_SIZE = 8192;
protected volatile byte[] buf;
public BufferedInputStream(InputStream in) {
this(in, DEFAULT_BUFFER_SIZE);
}
public synchronized int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count)
return -1;
}
return getBufIfOpen()[pos++] & 0xff;
}
// 缓冲相关实现...
}
// 典型装饰链构建
InputStream is = new DataInputStream(
new BufferedInputStream(
new FileInputStream("data.bin")));
对比维度 | 装饰者模式 | 适配器模式 |
---|---|---|
目的 | 增强功能 | 转换接口 |
关系 | 同接口 | 不同接口 |
透明度 | 透明装饰 | 不透明包装 |
@startuml
interface Subject {
+ request()
}
class RealSubject {
+ request()
}
class Proxy {
- realSubject: RealSubject
+ request()
}
class Decorator {
- component: Component
+ operation()
}
Subject <|.. RealSubject
Subject <|.. Proxy
Proxy o-- RealSubject
Component <|.. Decorator
@enduml
// 反例:错误地关闭了底层流
try (InputStream is = new BufferedInputStream(
new FileInputStream("file"))) {
is.close(); // 只需要关闭最外层流
}
传统IO装饰者模式的问题: - 同步阻塞模型 - 装饰层数过多时性能下降
NIO的改进: - Channel/Buffer新抽象 - 选择器非阻塞模型
Spring等框架中的装饰应用: - TransactionAwareCacheDecorator - HttpRequestWrapper
通过对Java IO源码的分析,我们可以深刻理解装饰者模式的核心价值: 1. 动态扩展的能力使得系统保持高度灵活性 2. 组合代替继承的设计哲学体现了开闭原则 3. 透明装饰的特性让客户端无需关心具体实现
这种模式在保持对象接口不变的前提下,为对象动态添加功能的方式,在Java生态中有着广泛的应用,理解其本质对于编写可扩展的代码具有重要意义。
扩展阅读: 1. 《设计模式:可复用面向对象软件的基础》GoF 2. Java IO源码包:java.io 3. JDK源码分析相关工具:IDEA、JAD等 “`
注:本文实际约3700字(含代码和图示),完整呈现了从理论到实践的装饰者模式解析。如需进一步扩展某些章节或添加具体案例,可以适当补充实际项目中的应用示例。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。