Decorating mode for Java IO

Source: Internet
Author: User

I have divided the decorative pattern into several parts myself:

1. Decorations

The Act of Decorating (behavior, method)

2. Objects to be decorated

The act of being decorated (behavior, method)

  Note: All decorations and objects to be decorated should have a common decorative interface or abstract class ( used to create an association between decorations and decorative objects, and can guarantee the order of decoration. ),

The Act of Decorating and the act of being decorated are different implementations of this interface or abstract class,

There is a call to the decorated action in the decoration action. (This requires that the decorations have a property of the object being decorated and that the initialization succeeds before invoking the action)

A simple example:

Use PrintWriter to write the contents of the array to the Test.txt file. Among them, PrintWriter, BufferedWriter is the adorner, FileWriter is the adornment object, writer is the public parent class, and writer's write method is used to realize the adornment action and the Adornment Action abstract method (writer# abstract public void write (char cbuf[], int off, int len) throws IOException; ).

1  Public Static voidWritefilewithio (int[] source) {2         Try(PrintWriter pw =NewPrintWriter (NewBufferedWriter (NewFileWriter ("Test.txt")))) {3              for(intO:source) {4 pw.println (string.valueof (o));5             }6}Catch(IOException e) {7 e.printstacktrace ();8         }9}

PrintWriter the relevant source code:

1  Public classPrintWriterextendsWriter {2 3     /**4 * The underlying character-output stream of this5 * <CODE>PRINTWRITER</CODE>.6      *7      * @since1.28      */9     protectedWriter out;Ten  One     /** A * Creates a new printwriter, without automatic line flushing. -      * -      * @paramOut A character-output stream the      */ -      PublicPrintWriter (Writer out) { -          This(Out,false); -     } +  -      PublicPrintWriter (Writer out, +                        BooleanAutoFlush) { A         Super(out); at          This. Out =Out ; -          This. AutoFlush =AutoFlush; -LineSeparator =java.security.AccessController.doPrivileged ( -             NewSun.security.action.GetPropertyAction ("Line.separator")); -     } -  in  -     /** to * Writes a string. This method cannot is inherited from the Writer class + * because it must suppress I/O exceptions. -      * @params String to be written the      */ *      Public voidWrite (String s) { $Write (s, 0, S.length ());Panax Notoginseng     } -      the     Public voidWrite (String s,intOffintLen) { +         Try { A             synchronized(lock) { the Ensureopen (); +                //this out object is the focus, before and after the code is "decorations" - Out.write (S, off, Len); $             } $         } -         Catch(Interruptedioexception x) { - Thread.CurrentThread (). interrupt (); the         } -         Catch(IOException x) {WuyiTrouble =true; the         } -     } Wu}

The Write method code in BufferedWriter:

1  Public classBufferedWriterextendsWriter {2 3     PrivateWriter out;4 5     Public voidWriteCharCbuf[],intOffintLenthrowsIOException {6         synchronized(lock) {7 Ensureopen ();8             if((Off < 0) | | (Off > Cbuf.length) | | (Len < 0) | |9((off + len) > cbuf.length) | | ((off + len) < 0)) {Ten                 Throw Newindexoutofboundsexception (); One}Else if(len = = 0) { A                 return; -             } -  the             if(Len >=nchars) { -                 /*If The request length exceeds the size of the output buffer, - flush the buffer and then write the data directly. in this - The buffered streams would cascade harmlessly.*/ + Flushbuffer (); -                 //the same is true for the Out object . + Out.write (Cbuf, off, Len); A                 return; at             } -  -             intb = off, t = off +Len; -              while(b <t) { -                 intd = min (Nchars-nextchar, T-b); - system.arraycopy (cbuf, B, CB, Nextchar, D); inB + =D; -Nextchar + =D; to                 if(Nextchar >=nchars) + Flushbuffer (); -             } the         } *     } $ Panax Notoginseng}

The FileWriter call is the writer's write method. It's not posted here. If there is an inaccurate place to welcome the point.

Decorating mode for Java IO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.