应用场景
装饰器模式主要解决继承关系过于复杂的问题,通过组合来替代继承。它主要的作用是给原始类添加增强功能。
装饰器模式有一个特点,那就是可以对原始类嵌套使用多个装饰器。为了满足这个应用场景,在设计的时候,装饰器类需要跟原始类继承相同的抽象类或者接口。
聊聊 Java IO 类
Java IO 类库非常庞大和复杂,有几十个类,负责 IO 数据的读取和写入。我们可以把 IO 类分为四类。
针对不同的读取和写入场景,Java IO 又在这四个父类基础之上,扩展出了很多子类。
相信大多数开发者在初次接触 Java IO 类时面对庞大的 Java IO 类家族产生困惑:为什么会有如此多的类?他们使用上有啥区别?
这里以读取 config.properties 文件为例,InputStream 是一个抽象类,FileInputStream 是专门用来读取文件流的子类。BufferedInputStream 是一个支持带缓存功能的数据读取类,可以提高数据读取的效率。
InputStream in = new FileInputStream("config.properties");InputStream bin = new BufferedInputStream(in);byte[] data = new byte[128];while (bin.read(data) != -1) { //...}
复制代码
Java IO 为什么不设计一个继承 FileInputStream 并且支持缓存的BufferedFileInputStream 类呢?这样我们就可以直接创建new BufferedFileInputStream() 简单许多。
继承的问题
事实上如果只是扩展这一个类,那还可以接受,但是InputStream具有很多子类,如果我们继续按照继承的方式来实现的话,就需要再继续派生出 DataFileInputStream、DataPipedInputStream 等类。如果我们还需要既支持缓存、又支持按照基本类型读取数据的类,那就要再继续派生出 BufferedDataFileInputStream、BufferedDataPipedInputStream 等 n 多类。这还只是附加了两个增强功能,如果我们需要附加更多的增强功能,那就会导致组合爆炸,类继承结构变得无比复杂,代码既不好扩展,也不好维护。
所以 Java 的设计者没有使用继承的方式来进行扩展,而是使用组合,这里也体现了组合优于继承的设计原则。下面是简化的 JDK 源码
public abstract class InputStream { // ...}
public class BufferedInputStream extends InputStream { protected volatile InputStream in; public BufferedInputStream(InputStream in) { this.in = in; } // 实现基于缓存的读取...
}
public class DataInputStream extends InputStream { protected volatile InputStream in; public BufferedInputStream(InputStream in) { this.in = in; } // 实现读取基本数据类型...
}
复制代码
如果去查看 JDK 的源码,你会发现,BufferedInputStream、DataInputStream 并非继承自 InputStream,而是另外一个叫 FilterInputStream 的类。
这是因为 InputStream 是一个抽象类,其中很多方法已经有了默认实现。我们想复用这些实现,但是我们是通过在构造函数注入 InputStream,然后赋值给成员变量InputStream in,将所有功能委托给成员变量in来实现的,因此抽象类 InputStream 的方法即使被继承,调用的对象也是当前类实例,而不是委托给in,所以需要有一个装饰器类FilterInputStream。
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 int read(byte b[]) throws IOException { return read(b, 0, b.length); } public int read(byte b[], int off, int len) throws IOException { return in.read(b, off, len); } public long skip(long n) throws IOException { return in.skip(n); } public int available() throws IOException { return in.available(); } public void close() throws IOException { in.close(); } public synchronized void mark(int readlimit) { in.mark(readlimit); } public synchronized void reset() throws IOException { in.reset(); } public boolean markSupported() { return in.markSupported(); }}
复制代码
装饰器模式是简单的“用组合替代继承”吗?
当然不是。从 Java IO 的设计来看,装饰器模式有两个比较特殊的地方:
InputStream in = new FileInputStream("content.txt");InputStream bin = new BufferedInputStream(in);DataInputStream din = new DataInputStream(bin);int data = din.readInt();
复制代码
跟代理模式的区别
代理模式中,代理类附加的是跟原始类无关的功能,而在装饰器模式中,装饰器类附加的是跟原始类相关的增强功能。
类图
代码实现
Shape
public abstract class Shape {
public abstract void draw();
public abstract void doOther();
}
复制代码
Circle
public class Circle extends Shape {
@Override public void draw() { System.out.println("You are drawing circle..."); }
@Override public void doOther() { System.out.println("You are doing other..."); }
}
复制代码
Square
public class Square extends Shape {
@Override public void draw() { System.out.println("You are drawing square..."); }
@Override public void doOther() { System.out.println("You are doing other..."); }
}
复制代码
ShapeDecorator
public class ShapeDecorator extends Shape {
protected Shape shape;
public ShapeDecorator(Shape shape) { this.shape = shape; }
@Override public void draw() { shape.draw(); }
@Override public void doOther() { shape.doOther(); }
}
复制代码
RedShapeDecorator
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape shape) { super(shape); }
@Override public void draw() { shape.draw(); System.out.println("The shape color is red"); }
}
复制代码
Main
public class Main {
public static void main(String[] args) { Shape shape; RedShapeDecorator decorator;
shape = new Circle(); decorator = new RedShapeDecorator(shape); decorator.draw(); decorator.doOther();
shape = new Square(); decorator = new RedShapeDecorator(shape); decorator.draw(); decorator.doOther(); }
}
复制代码
评论