Design Pattern _ decorator _ decoration pattern

Source: Internet
Author: User

Image example:

Mary should not ask her to pick her up after her birthday. Otherwise, the food expenses for this month will surely be finished. She took out the photo I took on the top of Huashan last year and wrote "the best gift" on the back, I love your FITA. "I went to the gift shop on the street and bought an image frame (the mm for selling gifts is also very beautiful ), mike, who is next door to art design, designed a beautiful box and installed it ......, We are all decorator, and we are all modifying this person. How do you understand this?

Decoration mode:

The decoration mode extends the object functions in a transparent way to the client. It is an alternative to the inheritance relationship and provides more flexibility than inheritance. Dynamically adds features to an object, which can be dynamically revoked. A large number of functions are generated by the arrangement and combination of some basic functions.

Dynamically add some additional responsibilities to an object. The decorator mode is more flexible than the subclass generation function.

UML structure diagram:

Abstract base class:
1) component: defines an object interface, which can dynamically add responsibilities to this interface.
2) decorator: maintains a pointer to component and has an interface function consistent with component.

Interface functions:
1) component: Operation: this interface function is declared by component. Therefore, all the derived classes of component must be implemented. You can dynamically add responsibilities to this interface function.

The derived class of the decorator can dynamically add roles to the objects of the concreatecomponent class, or you can say that the derived class of the decorator is decorated with objects of the concreatecomponent class. specifically, initialize a concreatecomponent Class Object (decorator) and use this object to generate a decorator object (decorator ), the subsequent call to the operation function is a multi-state call to the member function of the decorator object. the main point of implementation here is that both the decorator class and the concreatecomponent class inherit from component, so that the interface functions of both are consistent. Secondly, decorator maintains a pointer to component, this allows you to call the component: operation function dynamically.


Design Pattern _ decorator _ decoration 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: 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.