Java: Decorative Design Patterns

Source: Internet
Author: User

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 existing functionality,and provide enhanced functionality, then the custom class is called the adornment class. decorative classes typically receive decorated objects by constructing methods, and are based on the functions of the decorated objects, providing a strongerfunction. ///Example 1:
classperson{ Public voidChifan () {System.out.println (Eat); }}classsuperperson{PrivatePerson p;  PublicSuperperson (person p) { This. P =p; }     Public voidChifan () {System.out.println ("Aperitif");        P.chifan (); System.out.println (Dessert); System.out.println ("Smoke One."); }}classpersondemo{ Public Static voidMain (string[] args) {person P=NewPerson (); //P.chifan ();Superperson SP =NewSuperperson (P);    Sp.chifan (); }}

the difference between decoration and inheritance:before the feature is enhanced:myreader//classes that are designed to read data. |--mytextreader|--mybufferedtextreader|--mymediareader|--mybufferedmedireader|--mydatareader|--mybuffereddatareaderFeature Enhancements:class Mybufferedreader{
Mybufferedreader (mytextreader text) {} Mybufferedreader (mymediareader media) {} Mybufferedreader (mydatareader data) {}
}//The above class has poor extensibility. find the common type of its parameters. In polymorphic form, scalability can be improved. class Mybufferedreader extends myreader{
Private myreader R; Mybufferedreader (myreader R) {  }
}after the feature is enhanced:myreader//classes that are designed to read data. |--mytextreader|--mymediareader|--mydatareader|--mybufferreader decoration mode is more flexible than inheritance, and avoids the bloated inheritance system. It also reduces the relationship between classes and classes. Decoration class because of the enhancement of existing objects, has the same functionality and existing is the same, but provides more powerful features. So decorative and decorated classes are usually in the same individual system.  

Java: Decorative Design Patterns

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.