Java decoration mode

Source: Internet
Author: User

In Java IO is the use of decorative mode design. One layer of decorative layer

such as: DataOutputStream dos = new DataOutputStream (new Bufferedoutputstream (New FileOutputStream ("Data.txt"));

Composition of the decoration mode: 1, abstract component role: OutputStream or InputStream in fairly java.io

2, the specific component role: equivalent to java.io in the FileInputStream or FileOutputStream

3. Abstract decorative Role: holds a reference to a component (Component) object, and define an interface that is consistent with the abstract component interface (that is, the role inherits or implements the abstract component role) equivalent to FilterInputStream or Filteroutputstream in java.io.

4, the specific decorative role: responsible for adding additional functions to the component objects. Equivalent to java.io bufferedinputstream or Bufferedoutputstream.

The code is implemented as follows:

 /**  * defines the abstract component role and defines a method  */ public  interface   Component { public  void   DoSomething ();}  
/** * Define specific component roles and implement methods */  Public class Implements Component {    @Override    publicvoid  dosomething () {        System.out.println ("FileInputStream");}    }
/*** Define abstract decorative roles, hold references to artifact objects, and define an interface that is consistent with the abstract component interface (that is, the role inherits or implements the abstract component role) *@authorAcer **/ Public classDerectorImplementscomponent{PrivateComponent Component;  Publicderector (Component Component) { This. comonent =component; } @Override Public voiddosomething () {component.dosomething (); }}
/*** Define specific decorative roles and add additional features *@authorAcer **/ Public classConcreteDerector1extendsDerector { PublicConcreteDerector1 (compenent component) {Super(component); } @Override Public voiddosomething () {Super. dosomething ();    Doanything (); }         Public voiddoanything () {System.out.println ("DataInputStream"); }}
 /**   * decoration mode, client Test * *   @author   Acer *  */ public  class   Client { public  static  void   main (string[] args) {Component Component1  = new   Concretecomponent (); Component component2  = new  ConcreteDerector1 (component1); //  build datainputstream   component2.d    Osomething (); }}

Output Result:

FileInputStream
DataInputStream

Java decoration 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.