Decorative design mode

Source: Internet
Author: User

Decorative design pattern

1. Decorative design mode:

    • When you want to enhance the functionality of an existing object, you can define a class, pass in an existing object, and, based on the functionality of an existing object, and provide enhancements, the defined class is called an ornament class;
    • The adornment class usually receives the decorated object through the constructor function, and provides the stronger function based on the function of the decorated object;

such as: BufferedStream (InputStream in);

BufferedStream is a decorative class based on InputStream.

2. The difference between decoration and inheritance:

    • decoration class because of the enhancement of existing objects, has the same function and has the same, but provides more powerful function; Decorative and decorated classes usually belong to the same system;
/* * Customize a decorated class with line number Mylinenumberreader */package unit15;import java.io.*;class Mylinenumberreader extends mybufferedreader{//Decoration class private Reader r;private int linenumber; Mylinenumberreader (reader R) {///Based on polymorphic thought, use Reader as the Parameter object class super (r);} Public String Mylinenumberreaderline () throws ioexception{     linenumber++;     Return super. Myreadline ();} public void Setlinenumber (int linenumber) {this.linenumber = linenumber;} public int Getlinenumber () {return linenumber;} public void Myclose () throws Ioexception{r.close ();}} public class Mylinenumbertest {public static void main (String args[]) {try {FileReader fr = new FileReader ("Src/unit15/file Writertest.java "); Mylinenumberreader MLR = new Mylinenumberreader (FR); String str = null;while ((STR=MLR). Mylinenumberreaderline ())!=null) {System.out.println (str);} Mlr. Myclose ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

3. The application of decoration design pattern is most common in IO input/output, and two of them are more important:

    • InputStreamReader (InputStream in);
    • Outputsteamwriter (OutputStream out); Convert Stream

This method is very useful in keyboard input and output:

Example:

1  Packageunit15;2 3 ImportJava.io.*;4 5  Public classTransfreamtest {6      Public Static voidMain (String args[]) {7InputStream in =system.in;8InputStreamReader ISR =NewInputStreamReader (in);9BufferedReader BFR =NewBufferedReader (ISR);TenBufferedreadser buf = new BufferedReader (new Inputsream (system.in));//keyboard Input combination format OneOutputStream out =System.out; AOutputStreamWriter OSW =NewOutputStreamWriter (out); -BufferedWriter BRW =NewBufferedWriter (OSW); -BufferedWriter BFW = new BufferedWriter (new Outputsteam (System.out));//Output to console the          -String str =NULL; -         Try { -              while((Str=bfr.readline ())! =NULL){ +                 if("Over". Equals (str)) -                      Break; +                 //System.out.println (str); A brw.write (str); at brw.newline (); - Brw.flush (); -                  -             } - bfr.close (); - brw.close (); in}Catch(IOException e) { -             //TODO auto-generated Catch block to e.printstacktrace (); +         } -              the     } *  $}

Decorative design mode

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.