1. 程式人生 > >Java IO的裝飾模式

Java IO的裝飾模式

with cti direct tput write fcc without index rop

我自己把裝飾模式分為了幾部分:

  1.裝飾品

    裝飾的動作(行為,方法)  

  2.被裝飾的對象

    被裝飾的動作(行為,方法) 

  註:所有的裝飾品和被裝飾的對象要有一個共同的裝飾接口或抽象類(用來建立裝飾品和裝飾對象的關聯關系,並且可以保證裝飾的順序。),

    裝飾的動作和被裝飾的動作是這個接口或者抽象類的不同實現,

    裝飾的動作中有對被裝飾動作的調用。(這就要求裝飾品中有一個被裝飾的對象的屬性,並且在調用動作之前初始化成功)

簡單的例子:

  使用PrintWriter將數組中的內容寫入到test.txt文件中。其中PrintWriter、BufferedWriter是裝飾器,FileWriter是被裝飾對象,Writer是公共的父類,而Writer中的write方法就是用來實現裝飾動作和被裝飾動作的抽象方法(Writer#abstract public void write(char cbuf[], int off, int len) throws IOException;

)。

1 public static void writeFileWithIO(int[] source){
2         try (PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")))) {
3             for (int o : source) {
4                 pw.println(String.valueOf(o));
5             }
6         } catch (IOException e) {
7             e.printStackTrace();
8 } 9 }

PrintWriter的相關源碼:

 1 public class PrintWriter extends Writer {
 2 
 3     /**
 4      * The underlying character-output stream of this
 5      * <code>PrintWriter</code>.
 6      *
 7      * @since 1.2
 8      */
 9     protected Writer out;
10 
11     /**
12      * Creates a new PrintWriter, without automatic line flushing.
13 * 14 * @param out A character-output stream 15 */ 16 public PrintWriter (Writer out) { 17 this(out, false); 18 } 19 20 public PrintWriter(Writer out, 21 boolean autoFlush) { 22 super(out); 23 this.out = out; 24 this.autoFlush = autoFlush; 25 lineSeparator = java.security.AccessController.doPrivileged( 26 new sun.security.action.GetPropertyAction("line.separator")); 27 } 28 29 30 /** 31 * Writes a string. This method cannot be inherited from the Writer class 32 * because it must suppress I/O exceptions. 33 * @param s String to be written 34 */ 35 public void write(String s) { 36 write(s, 0, s.length()); 37 } 38 39 public void write(String s, int off, int len) { 40 try { 41 synchronized (lock) { 42 ensureOpen(); 43 //這個out對象就是重點,之前和之後的代碼都是“裝飾品” 44 out.write(s, off, len); 45 } 46 } 47 catch (InterruptedIOException x) { 48 Thread.currentThread().interrupt(); 49 } 50 catch (IOException x) { 51 trouble = true; 52 } 53 } 54 }

BufferedWriter中的write方法代碼:

 1 public class BufferedWriter extends Writer {
 2 
 3     private Writer out;
 4 
 5    public void write(char cbuf[], int off, int len) throws IOException {
 6         synchronized (lock) {
 7             ensureOpen();
 8             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
 9                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
10                 throw new IndexOutOfBoundsException();
11             } else if (len == 0) {
12                 return;
13             }
14 
15             if (len >= nChars) {
16                 /* If the request length exceeds the size of the output buffer,
17                    flush the buffer and then write the data directly.  In this
18                    way buffered streams will cascade harmlessly. */
19                 flushBuffer();
20                 //這個out對象也是一樣
21                 out.write(cbuf, off, len);
22                 return;
23             }
24 
25             int b = off, t = off + len;
26             while (b < t) {
27                 int d = min(nChars - nextChar, t - b);
28                 System.arraycopy(cbuf, b, cb, nextChar, d);
29                 b += d;
30                 nextChar += d;
31                 if (nextChar >= nChars)
32                     flushBuffer();
33             }
34         }
35     }
36 
37 }

FileWriter調用的是Writer的write方法。在這裏就不貼出來了。如果有不準確的地方歡迎指出。

Java IO的裝飾模式