Interpreting design Patterns----decorative patterns (decorator pattern)

Source: Internet
Author: User
Tags wrapper

The decoration mode (decorator) is also called the wrapper mode (wrapper). The idea of "decorating" vividly depicts the intention to "dynamically add some additional responsibilities" to an object. Gof's definition in design mode is: Dynamically adding additional responsibilities to an object. The decorative mode makes full use of the advantages of inheritance and aggregation, and creates the design aesthetics of no and ratio. Decorator mode is more flexible than generating subclasses in terms of adding functionality.

UML diagram:

I. Motivation to use the decorator model

Now there is a scenario that requires us to dynamically add new responsibilities to an object that does not modify the original behavior, but instead adds new functionality based on the original behavior, just as we do with fresh and tasty tomato juice when eating french fries.

From an object-oriented perspective, we have to add a new responsibility for an object that can be implemented using inheritance mechanisms, but such a design can lead to the problem of "overly using inheritance to extend the functionality of objects" because of the static nature introduced by inheritance, which makes this extension inflexible; and as word classes grow, (expanded functionality), the combination of seed classes (the combination of extended functions) can cause more subclasses to swell, known as class explosions. How can the extension of object functionality be dynamically implemented as needed while avoiding the explosion of "extended functionality"? So that any "functional expansion changes" will result in the lowest impact?

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.