Decorative design pattern

Source: Internet
Author: User

Decorative design mode:

When you want to enhance the functionality of an existing object, you can customize the class, pass in existing objects, and provide enhanced functionality based on the functionality of the existing objects. Then the custom class is called the adornment class.

The adornment class usually receives the decorated object by constructing the method. It provides enhanced functionality based on the functionality of the object being decorated.

myreader//A class that is dedicated to reading data.
|--mytextreader
|--mybuffertextreader
|--mymediareader
|--mybuffermediareader
|--mydatareader
|--mybufferdatareader

Class Mybufferreader
{
Mybufferreader (mytextreader text)
{}
Mybufferreader (Mymediareader Media)
{}
}
The above class has very poor extensibility.
Find the common type of its parameters. Through polymorphic forms. can improve scalability.

Class Mybufferreader extends Myreader
{
Private myreader R;
Mybufferreader (myreader R)
{}
}


myreader//A class that is dedicated to reading data.
|--mytextreader
|--mymediareader
|--mydatareader
|--mybufferreader


Previously, each subclass was buffered through inheritance.
Then the inheritance system will be complex and not conducive to expansion.

Now optimize your mind. Describe the buffer content separately.
The object that will need to be buffered. Passed in. That is, who needs to be buffered, and who is passed as a parameter to the buffer.
So the inheritance system becomes very simple. Optimized the architecture.

Decoration mode is more flexible than inheritance. Avoid 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 objects are the same, but provides more powerful features.
So decorative and decorated classes are usually part of a system.

Demo:

classperson{ Public voidChifan () {System.out.println (Eat); }}classSuperperson {PrivatePerson p; Superperson (person p) { This. P =p; }     Public voidSuperchifan () {System.out.println ("Aperitif");        P.chifan (); System.out.println (Dessert); System.out.println ("Take one."); }}classpersondemo{ Public Static voidMain (string[] args) {person P=NewPerson (); //P.chifan ();Superperson SP=NewSuperperson (P);    Sp.superchifan (); }}
View Code

Decorative design pattern

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.