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 {}
public int read(byte b[], int off, int len) throws IOException {}
public long skip(long n) throws IOException {}
public int available() throws IOException {}
public void close() throws IOException {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {}
public boolean markSupported() {}
}
下面是作为装饰模式的抽象装饰角色 FilterInputStream 类的源代码。可以看出,FilterInputStream 的接口与 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();
}
}
下面是具体装饰角色 PushbackInputStream 的源代码。
public class PushbackInputStream extends FilterInputStream {
private void ensureOpen() throws IOException {}
public PushbackInputStream(InputStream in, int size) {}
public PushbackInputStream(InputStream in) {}
public int read() throws IOException {}
public int read(byte[] b, int off, int len) throws IOException {}
public void unread(int b) throws IOException {}
public void unread(byte[] b, int off, int len) throws IOException {}
public void unread(byte[] b) throws IOException {}
public int available() throws IOException {}
public long skip(long n) throws IOException {}
public boolean markSupported() {}
public synchronized void mark(int readlimit) {}
public synchronized void reset() throws IOException {}
public synchronized void close() throws IOException {}
}
查看源码可以发现,这个装饰类提供了额外的方法 unread(),这就意味着 PushbackInputStream 是一个半透明的装饰类。换言 之,它破坏了理想的装饰模式的要求。如果客户端持有一个类型为 InputStream 对象的引用 in 的话,那么如果 in 的真实类型是 PushbackInputStream 的话,只要客户端不需要使用 unread() 方法,那么客户端一般没有问题。但是如果客户端必须使用这个方法,就必须进行向下类型转换。将 in 的类型转换成为 PushbackInputStream 之后才可能调用这个方法。但是这个类型转换意味着客户端必须知道它拿到的引用是指向一个类型为 PushbackInputStream 的对象,这就破坏了使用装饰模式的原始用意。
public static void main(String[] args) {
Color white = new TestStructDesignModel().new White();
Shape square = new TestStructDesignModel().new Square();
square.setColor(white);
square.draw();
Shape rectange = new TestStructDesignModel().new Rectangle();
rectange.setColor(white);
rectange.draw();
}
总结:
@Override
public void doWork() {
System.out.println("coder" + name + " do work");
}
}
private static final Map<String, IPerson> persons = new HashMap<>();
private static final class PersonFactory {
private static final Map<String, IPerson> personMap = new HashMap<>();
public static IPerson get(String name) {
IPerson person = personMap.get(name);
if (person == null) {
person = new Coder(name);
personMap.put(name, person);
}
return person;
}
public static int getSize() {
return personMap.size();
}
}
public static void main(String[] args) {
String name = "kzw";
for (int i = 1; i <= 10; i++) {
IPerson person = PersonFactory.get(name);
person.doWork();
System.out.println("对象池中对象数量为:" + PersonFactory.getSize());
}
}
执行输出:
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1
coderkzw do work
对象池中对象数量为:1