Curriculum design patterns Design patterns succinctly 11-3 decorator pattern parsing source code

1 Source resolve

1.1 Analytical source 1 (the application of a jdk1.7)

1.2 Analytical source (2 applications of two jdk1.7 )

1.3 Source resolution (applications in Spring) 3

1.4 Analytical source 4 (application of Servlet)

1.5 Analytical source 5 (in application myBaties)

 

 

1 Source resolve
1.1 Analytical source 1 (the application of a 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 Analytical source (2 applications of two jdk1.7 )

FIG class uml

 

Decorative parent subclass 1: BufferedInputStream

public 
class  BufferedInputStream the extends FilterInputStream {
 
// incoming parent parent class
public BufferedInputStream (in the InputStream) { the this (in, DefaultBufferSize); } / ** * realize data stream read, play a decorative role * / Private void Fill () throws IOException { byte [] = Buffer getBufIfOpen (); IF (markpos <0 ) POS = 0; / * NO Mark: the throw Away The Buffer * / the 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; } }

 

Decorative parent 1: FilterInputStream

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

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

}

 

Decorated class: InputStream

public abstract class InputStream implements Closeable {

}

 

Decorative parent class 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 Source resolution (applications in Spring) 3

TransactionAwareCacheDecorator

/ ** 
   * This class is the class cache storage and related matters 
   * / 
public  class TransactionAwareCacheDecorator the implements the Cache {
     Private  Final the Cache  targetCache; 

    public TransactionAwareCacheDecorator ( the Cache  targetCache) { 
        Assert.notNull (targetCache, "the Target Not the Cache MUST BE null" );
         the this .targetCache = targetCache; 
    } 
}

 

1.4 Analytical source 4 (application of Servlet)

 

1.5 Analytical source 5 (in application 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;
  }
}

 

Guess you like

Origin www.cnblogs.com/1446358788-qq/p/11489540.html