Differences between decoration and inheritance

Source: Internet
Author: User

Sub-classes use the same technology. Is it necessary to separate them into sub-classes?

This is why the decoration mode is generated.

 

Class mybufferreader {

Mybufferedreader (class 1 class to improve efficiency ){}

Mybufferedreader (Class 2 class to improve efficiency ){}

Mybufferedreader (Class 3 class to improve efficiency ){}

}

--------------------------------

Code A after optimization:

Class mybufferreader extends parent class {

Private parent class;

Mybufferedreader (parent class ){}

// Combination Structure

}

---------------------------------

Decoration mode, which is more flexible than inheritance; reduces the relationship between classes.

Because the decoration class enhances existing objects, it has the same functions as existing objects, but provides stronger functions. Therefore, the decoration class and the decoration class usually belong to a system.

 

-----------------------

Decoration class, covering the abstract methods in the reader class

Public void close (){

Class. Close ();

}

 

Public int read (char [] Buf, int off, int Len ){

Return R. Read (BUF, off, Len );

}

 

---------------------------------

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.