装饰模式(Decorator)

当我们要为一个对象添加新的方法时,我们一般有两种方式:

  • 继承机制 通过继承一个现有类使得子类也拥有父类的方法,然后在子类中扩展新方法。
  • 关联机制 将一个类的对象实例化后嵌入到新的对象中,由新对象来决定是否调用嵌入的对象的方法来扩展自己的行为。这个被嵌入的对象被称为装饰器(Decorator)。

装饰模式又称为包装器(Wrapper):对客户透明的方式动态的给一个对象附加上更多的责任。“对客户透明的方式”:指的是客户就像看不见一样,也就是客户使用的时候不必关心对象装饰前和装饰后有什么不同,装饰模式最典型的应用是Java的I/O流。本文中涉及的代码请点击下载
先看看InputStream部分结构图:

InputStream继承关系
InputStream继承关系

InputStreamInputStream是个抽象类,其中read()方法又是抽象方法。

InputStream
InputStream

然后我们看FilterInputStream,首先FilterInputStream extends InputStream,然后在构造方法中传入InputStream,实现read()方法,并重写read(byte b[], int off, int len)方法。

protected volatile InputStream in;
protected FilterInputStream(InputStream in) {
    this.in = in;
}
public int read() throws IOException {
    return in.read();
}
public int read(byte b[], int off, int len) throws IOException {
    return in.read(b, off, len);
}

FilterInputStream的作用是在的InputStream前加一层过滤,它的方法结构图如下:

空心三角形代表实现  实心三角形代表重载
空心三角形代表实现 实心三角形代表重载

最后看看DataInputStreamBufferedInputStream的结构图,他们都是继承自FilterInputStream

DataInputStream&BufferedInputStream
DataInputStream&BufferedInputStream

回到之前说对客户透明,对比下面两种实例化新对象的方式,前后没有多大的区别:

BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("IODecorator.txt"));
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("IODecorator.txt"));

由此可知装饰器的UML图:

装饰器模式UML图
装饰器模式UML图

Component:抽象构件(相当于InputStream)
ConcreteComponent:具体构件(相当于FileInputStream 直接继承自InputStream)
Decorator:抽象装饰类(相当于FilterInputStream)
ConcreteDecorator:具体装饰类(相当于DataInputStream或BufferedInputStream)

说了输入流,相似的我们再通过一个例子来讲讲输出流。假设现在有个需求是将一段文字加密之后输出到另外一个文件中。假设我们的加密规则如下:将英文字母后移两位,即:a变成c,b变成d,依此类推,最后y变成a,z变成b(为简单我们只处理小写字母)。
为此我们写个类来装饰OutputStream

public class IODecorator {
    public static void main(String[] args) {
        DataInputStream dataInputStream = null;
        try {
            dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream("src/Decorator/IODecorator.txt")));

            byte bs[] = new byte[dataInputStream.available()];
            dataInputStream.read(bs);
            String content = new String(bs);
            System.out.println("文件内容:" + content);
        } catch (Exception e) {
            // TODO: handle exception
        } 
    }
}

写完后测试一下:

DataOutputStream dos = new DataOutputStream(
                new BufferedOutputStream(
                        new EncryptOutputStream(
                                new FileOutputStream("src/Decorator/IODecorator.txt"))));
        dos.write("helloworldxyz".getBytes());
        dos.close();

得到的结果是:jgnnqyqtnfzab。加密成功!
装饰模式的本质是:动态组合。所以它比继承更灵活,但是装饰模式相比继承会产生更多细粒度的东西。因为装饰模式是把一系列复杂的功能分散到各个装饰器中,一般一个装饰器只实现一个功能,这样会产生很多细粒度的对象,功能越复杂,需要细粒度的对象越多!
一般在以下情况下使用装饰模式:

  • 如果需要在不影响其他对象的情况下,以动态透明的方式给对象添加职责,可以使用装饰模式。
  • 如果子类不适合扩展,比如扩展功能需要的子类太多,可以使用装饰模式来组合对象。

源码下载:http://pan.baidu.com/s/1nt27ukh

原文地址:http://www.jianshu.com/p/24d88fb3fe18

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值