这里有篇文章对装饰器做了很好的说明:装饰器模式(装饰设计模式)详解
借助这篇文章的解释,装饰器模式主要包含以下角色。
- 抽象构件(Component)角色:定义一个抽象接口以规范准备接收附加责任的对象。
- 具体构件(ConcreteComponent)角色:实现抽象构件,通过装饰角色为其添加一些职责。
- 抽象装饰(Decorator)角色:继承抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。
- 具体装饰(ConcreteDecorator)角色:实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。
InputStream 类是一个抽象构建角色(Component)
public abstract class InputStream implements Closeable {
private static final int MAX_SKIP_BUFFER_SIZE = 2048;
public abstract int read() throws IOException;
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
public int read(byte b[], int off, int len) throws IOException {
……
}
public long skip(long n) throws IOException {
……
}
public int available() throws IOException {
return 0;
}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {
throw new IOException("mark/reset not supported");
}
public boolean markSupported() {
return false;
}
}
FilterInputStream 类是一个抽象装饰(Decorator)角色,即装饰器
//继承了抽象构建角色InputStream
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();
}
}
BufferedInputStream 就是一个装饰器的实现(ConcreteDecorator)
FileInputStream 和 BufferedInputStream 都是从一个文件读取数据,只是FileInputStream 是从硬盘直接读,而 BufferedInputStream多了一个缓冲区,执行read时先从缓冲区读取,当缓冲区数据读完时再把缓冲区填满。
所以FileInputStream 就是一个具体的构建角色(ConcreteComponent),BufferedInputStream 在原来读取方法的基础上增加了缓冲区的功能。通过装饰器模式实现了给具体构件对象添加附加责任的功能。