设计模式--Decorator 之 java.io

还记得被java io支配的恐惧吗,当读到core java 中的流与文件一章时,我整个人是懵逼的,为什么会设计这么多的类,怎么会记得清楚。直到我知道了Decorator设计模式,才知道了其中的用意所在,也才了解到了之前有人说java io设计的充满学院风的真意。
下面这张图是截取的core java volume II 中 Stream and Files一章中InputStream类图。
java InputStream 类图
事实上,FilterInputStream是一个装饰器的基类,第三行中所有的类都继承自这一个类。而第二行中其他的类则是可以被装饰的具体的组件类。
当我们想要读取File中的数据时我们可以这么干:

      FileInputStream fin = new FileInputStream("path");
      DataInputStream din = new DataInputStream(fin);
      double s = din.readDouble();

又或者我们想要构造一个既可以预读(PushBackInputStream)又可以使用读取数据的缓存输入流,那么我们需要这样做:

        DataInputStream din = new DataInputStream(
                pin = new PushbackInputStream(
                new BufferedInputStream(
                        new FileInputStream("path"))));

其中,FileInputStream是被装饰的组件,而外层包装的BufferedInputStream, PushbackInputStream, DataInputStream都是装饰器类。
对于OuputStream以及ReaderWriter类型的设计都和这里相似。

这里写图片描述

这里的FilterOutputStream即装饰器的基类。

看到这里,是不是手痒想写一个自己的装饰器类呢(并没有手痒,只有手累)。
现在我们实现一个将所有的输入都转换成小写的装饰器类:


import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class LowerCaseInputStream extends FilterInputStream{

    protected LowerCaseInputStream(InputStream in) {
        super(in);
    }

    public int read() throws IOException{
        int c = super.read();
        return (c== -1 ? c : Character.toLowerCase(c));
    }

    public int read(byte[] b, int offset, int len) throws IOException{
        int result = super.read(b, offset, len);
        for(int i = offset; i< offset+result; i++){
            b[i] = (byte)Character.toLowerCase((char)b[i]);

        }

        return result;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值