Java IO 中的装饰者(Decorator)模式(一)

一直对java.io一知半解,每到用时便google下例子代码----拿来主义,已是我之痼疾。
这几日便得了闲,拿了JDK 5的Doc、Thinking In Java(4th)和大话设计模式, 对照着JDK 5的源代码
开始看,翻来覆去几回,稍稍有了点眉目,便就上来得瑟一回,记个笔记。

--------------------------------------------------------------------------------------------
[size=medium][b]一. 从InputStream 及其子类的结构入手[/b][/size]

java.io中类数众多,名称相似,一眼看去,已是花了,至于哪些是输入相关的基本类,哪些又是装饰者类
就分不清了。所以我们就先来理一理其中的结构:

[b]java.io中的类大致分为两个部分:[/b]
1. InputStream、OutputStream --- 是对字节序列进行操作的抽象类,由JDK1.0发布。
2. Reader、Writer --- 是基于Unicode Code Unit 进行操作的抽象类, 由JDK1.1发布。

[b]我们今天先从InputStream入手,参考JDK文档画出了它的UML 类图(请从附件下载,图的篇幅较宽,就不贴了)[/b]。

[color=blue][b]图解[/b][/color] ---参考附件中图的左边部分(InputStream),右边是Reader:

[color=blue][b] 1. 输入相关的抽象超类:[/b][/color]java.io.InputStream

[color=blue][b] 2. 输入相关的基本类:[/b][/color]继承自java.io.InputStream,具体使用方法请参考JDK文档,但大致可以从其名字中看出其含义。总共有8个:
 
ByteArrayInputStream
PipedInputStream
SequenceInputStream
FileInputStream
ObjectInputStream
StringBufferInputStream(已经过时,由StringReader代替)
javax.sound.sampled.AudioInputStream(外部类)
org.omg.CORBA.portable.InputStream(外部类)


[color=blue][b] 3. 装饰者超类:[/b][/color]FilterInputStream,同样也继承自java.io.InputStream

[color=blue][b] 4. 装饰者子类:[/b][/color]继承自FilterInputStream,用于装饰上面的[b]"2. 输入相关的基本类"[/b],共有4个:
 
BufferedInputStream
PushbackInputStream
DataInputStream
LineNumberInputStream(已经过时)


[size=medium][b]二. InputStream 和 装饰者模式[/b][/size]

众人皆知java的输入输出包"java.io"是一个装饰者模式的典型案例,这从下面这个代码就可以大致看出:

final BufferInputStream bin =
new BufferInputStream(new FileInputStream("C:\\test.log"));


但具体是怎样子的还是需要查看JDK源代码:

[color=blue] [b] 1. java.io.InputStream == 装饰者模式中的所有类(装饰者和被装饰者)的抽象超类[/b][/color]
public abstract class InputStream implements Closeable {
// 定义抽象方法:read
public abstract int read() throws IOException;
}



[color=blue][b]2.java.io.FileInputStream == 具体的被装饰者对象[/b][/color]
public class FileInputStream extends InputStream {
public FileInputStream(String name) throws FileNotFoundException {
this(name != null ? new File(name) : null);
}
// 调用本地read方法
public native int read() throws IOException;
}


[color=blue][b]3.java.io.FilterInputStream == 装饰者对象的超类[/b][/color]
public class FilterInputStream extends InputStream {
/**
* The input stream to be filtered.
*/
protected volatile InputStream in;

protected FilterInputStream(InputStream in) {
this.in = in;
}

// 装饰者超类的read方法
public int read() throws IOException {
return in.read();
}
}



[color=blue][b]4.java.io.BufferedInputStream == 具体的装饰者对象[/b][/color]
public class BufferedInputStream extends FilterInputStream {

public BufferedInputStream(InputStream in) {
this(in, defaultBufferSize);
}

public BufferedInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}

// 具体装饰者类的read方法
public synchronized int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count)
return -1;
}
return getBufIfOpen()[pos++] & 0xff;
}
}


有过模式设计经验的同学不难发现这是一个经典的装饰者模式。

下一次,我们来看看InputStream和Reader的关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值