设计模式课程 设计模式精讲 11-3 装饰者模式源码解析

1    源码解析

1.1  源码解析1(jdk1.7中的应用一)

1.2  源码解析2(jdk1.7中的应用二

1.3  源码解析3(Spring中的应用)

1.4  源码解析4(Servlet中的应用)

1.5  源码解析5(myBaties中的应用)

1    源码解析
1.1  源码解析1(jdk1.7中的应用一)

BufferedReader

/**
  *
  *  bufferedReader 继承了Reader,并把Reader组合到BufferedReader中
  */
public class BufferedReader extends Reader {

    private Reader in;

/**
     * Creates a buffering character-input stream that uses an input buffer of
     * the specified size.
     *
     * @param  in   A Reader
     * @param  sz   Input-buffer size
     *
     * @exception  IllegalArgumentException  If sz is <= 0
     */
    public BufferedReader(Reader in, int sz) {
        super(in);
        if (sz <= 0)
            throw new IllegalArgumentException("Buffer size <= 0");
        this.in = in;
        cb = new char[sz];
        nextChar = nChars = 0;
    }

}

Reader

/**
   *   Reader是抽象类    
   */ 
public abstract class Reader implements Readable, Closeable {

)

 

1.2  源码解析2(jdk1.7中的应用二

uml类图

装饰父类1的子类:BufferedInputStream

public
class BufferedInputStream extends FilterInputStream {

//传入父类的父类
public BufferedInputStream(InputStream in) { this(in, defaultBufferSize); } /** * 实现读取流数据,起到装饰的作用 */ private void fill() throws IOException { byte[] buffer = getBufIfOpen(); if (markpos < 0) pos = 0; /* no mark: throw away the buffer */ else if (pos >= buffer.length) /* no room left in buffer */ if (markpos > 0) { /* can throw away early part of the buffer */ int sz = pos - markpos; System.arraycopy(buffer, markpos, buffer, 0, sz); pos = sz; markpos = 0; } else if (buffer.length >= marklimit) { markpos = -1; /* buffer got too big, invalidate mark */ pos = 0; /* drop buffer contents */ } else { /* grow buffer */ int nsz = pos * 2; if (nsz > marklimit) nsz = marklimit; byte nbuf[] = new byte[nsz]; System.arraycopy(buffer, 0, nbuf, 0, pos); if (!bufUpdater.compareAndSet(this, buffer, nbuf)) { // Can't replace buf if there was an async close. // Note: This would need to be changed if fill() // is ever made accessible to multiple threads. // But for now, the only way CAS can fail is via close. // assert buf == null; throw new IOException("Stream closed"); } buffer = nbuf; } count = pos; int n = getInIfOpen().read(buffer, pos, buffer.length - pos); if (n > 0) count = n + pos; } }

装饰父类1:FilterInputStream

public
class FilterInputStream extends InputStream {
    /**
     * The input stream to be filtered.
     */
    protected volatile InputStream in;

    protected FilterInputStream(InputStream in) {
        this.in = in;
    }

}

被装饰类:InputStream

public abstract class InputStream implements Closeable {

}

装饰父类2:FileInputStream

public class FileInputStream extends InputStream
{
/*
 *  传入file,装饰成inputstream
*/
public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.incrementAndGetUseCount();
        this.path = name;
        open(name);
    }

}
1.3  源码解析3(Spring中的应用)

TransactionAwareCacheDecorator

/**
   *    该类是储存缓存和事务的相关类
   */
public class TransactionAwareCacheDecorator implements Cache {
    private final Cache targetCache;

    public TransactionAwareCacheDecorator(Cache targetCache) {
        Assert.notNull(targetCache, "Target Cache must not be null");
        this.targetCache = targetCache;
    }
}
1.4  源码解析4(Servlet中的应用)
1.5  源码解析5(myBaties中的应用)
public class FifoCache implements Cache {

  private final Cache delegate;
  private Deque<Object> keyList;
  private int size;


  public FifoCache(Cache delegate) {
    this.delegate = delegate;
    this.keyList = new LinkedList<Object>();
    this.size = 1024;
  }
}

猜你喜欢

转载自www.cnblogs.com/1446358788-qq/p/11489540.html
今日推荐