Article From:https://www.cnblogs.com/wesleyshare/p/9217513.html

I myself divided the decorative pattern into several parts.

  1.Ornament

    The act of decoration (behavior, method)

  2.Ornamental objects

    The act of being decorated (behavior, method)

  Note: all decorations and objects decorated should have a common decorative interface or abstract class.It is used to establish the relationship between decorating and decorating objects, and can guarantee the order of decoration.),

    Decorative actions and decorative actions are different implementations of this interface or abstract class.

    There is a call to the decorative action in the decoration action. This requires that there is an attribute of the decorated object in the decoration, and it is initialized successfully before the action is invoked.

 

Simple examples:

  Use PrintWriter to write the contents of the array into the test.txt file. Among them, PrintWriter and BufferedWriter are decorators, FileWriter is decorated objects, Writer is a public parent, and WThe write method in riter is an abstract method used to achieve decorative actions and decorative actions (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 }

PrintWriterRelated source code:

 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                //This out object is the focus, before and after the code is "decorations".
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 }

BufferedWriterThe write method code in it:

 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                 //The same out object is the same
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 }

FileWriterThe write method of Writer is called. It doesn’t come out here. If there is an inaccurate place to welcome it.

 

Link of this Article: The decorative pattern of Java IO

Leave a Reply

Your email address will not be published. Required fields are marked *