decorator在java中_Decorator模式在JAVA中的IO应用

OutputStream是一个抽象类,它是所有输出流的公共父类,其源代码如下:

以下是代码片段:

public abstract class OutputStream implements Closeable, Flushable {

public abstract void write(int b) throws IOException;

...

}

它定义了write(int b)的抽象方法。这相当于Decorator模式中的Component类。

ByteArrayOutputStream,FileOutputStream 和 PipedOutputStream 三个类都直接从OutputStream继承,以ByteArrayOutputStream为例:

以下是代码片段:

public class ByteArrayOutputStream extends OutputStream {

protected byte buf[];

protected int count;

public ByteArrayOutputStream() {

this(32);

}

public ByteArrayOutputStream(int size) {

if (size 〈 0) {

throw new IllegalArgumentException("Negative initial size: "

+ size);

}

buf = new byte[size];

}

public synchronized void write(int b) {

int newcount = count + 1;

if (newcount 〉 buf.length) {

byte newbuf[] = new byte[Math.max(buf.length 〈〈 1, newcount)];

System.arraycopy(buf, 0, newbuf, 0, count);

buf = newbuf;

}

buf[count] = (byte)b;

count = newcount;

}

...

}

它实现了OutputStream中的write(int b)方法,因此我们可以用来创建输出流的对象,并完成特定格式的输出。它相当于Decorator模式中的ConcreteComponent类。

接着来看一下FilterOutputStream,代码如下:

以下是代码片段:

public class FilterOutputStream extends OutputStream {

protected OutputStream out;

public FilterOutputStream(OutputStream out) {

this.out = out;

}

public void write(int b) throws IOException {

out.write(b);

}

...

}

同样,它也是从OutputStream继承。但是,它的构造函数很特别,需要传递一个OutputStream的引用给它,并且它将保存对此对象的引用。而如果没有具体的OutputStream对象存在,我们将无法创建FilterOutputStream。由于out既可以是指向FilterOutputStream类型的引用,也可以是指向ByteArrayOutputStream等具体输出流类的引用,因此使用多层嵌套的方式,我们可以为ByteArrayOutputStream添加多种装饰。这个FilterOutputStream类相当于Decorator模式中的Decorator类,它的write(int b)方法只是简单的调用了传入的流的write(int b)方法,而没有做更多的处理,因此它本质上没有对流进行装饰,所以继承它的子类必须覆盖此方法,以达到装饰的目的。

BufferedOutputStream 和 DataOutputStream是FilterOutputStream的两个子类,它们相当于Decorator模式中的ConcreteDecorator,并对传入的输出流做了不同的装饰。以BufferedOutputStream类为例:

以下是代码片段:

public class BufferedOutputStream extends FilterOutputStream {

...

private void flushBuffer() throws IOException {

if (count 〉 0) {

out.write(buf, 0, count);

count = 0;

}

}

public synchronized void write(int b) throws IOException {

if (count 〉= buf.length) {

flushBuffer();

}

buf[count++] = (byte)b;

}

...

}

这个类提供了一个缓存机制,等到缓存的容量达到一定的字节数时才写入输出流。首先它继承了FilterOutputStream,并且覆盖了父类的write(int b)方法,在调用输出流写出数据前都会检查缓存是否已满,如果未满,则不写。这样就实现了对输出流对象动态的添加新功能的目的。

下面,将使用Decorator模式,为IO写一个新的输出流。

自己写一个新的输出流

了解了OutputStream及其子类的结构原理后,我们可以写一个新的输出流,来添加新的功能。这部分中将给出一个新的输出流的例子,它将过滤待输出语句中的空格符号。比如需要输出"java io OutputStream",则过滤后的输出为"javaioOutputStream"。以下为SkipSpaceOutputStream类的代码:

以下是代码片段:

import java.io.FilterOutputStream;

import java.io.IOException;

import java.io.OutputStream;

/**

* A new output stream, which will check the space character

* and won’t write it to the output stream.

* @author Magic

*

*/

public class SkipSpaceOutputStream extends FilterOutputStream {

public SkipSpaceOutputStream(OutputStream out) {

super(out);

}

/**

* Rewrite the method in the parent class, and

* skip the space character.

*/

public void write(int b) throws IOException{

if(b!=’ ’){

super.write(b);

}

}

}

它从FilterOutputStream继承,并且重写了它的write(int b)方法。在write(int b)方法中首先对输入字符进行了检查,如果不是空格,则输出。

以下是一个测试程序:

以下是代码片段:

import java.io.BufferedInputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

/**

* Test the SkipSpaceOutputStream.

* @author Magic

*

*/

public class Test {

public static void main(String[] args){

byte[] buffer = new byte[1024];

/**

* Create input stream from the standard input.

*/

InputStream in = new BufferedInputStream(new DataInputStream(System.in));

/**

* write to the standard output.

*/

OutputStream out = new SkipSpaceOutputStream(new DataOutputStream(System.out));

try {

System.out.println("Please input your words: ");

int n = in.read(buffer,0,buffer.length);

for(int i=0;i〈n;i++){

out.write(buffer[i]);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

执行以上测试程序,将要求用户在console窗口中输入信息,程序将过滤掉信息中的空格,并将最后的结果输出到console窗口。比如:

以下是引用片段:

Please input your words:

a b c d e f

abcdef

总   结

在java.io包中,不仅OutputStream用到了Decorator设计模式,InputStream,Reader,Writer等都用到了此模式。而作为一个灵活的,可扩展的类库,JDK中使用了大量的设计模式,比如在Swing包中的MVC模式,RMI中的Proxy模式等等。对于JDK中模式的研究不仅能加深对于模式的理解,而且还有利于更透彻的了解类库的结构和组成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值