Decorator-Design Mode Study Notes

Source: Internet
Author: User

Decoration Mode

Decoration mode: dynamically add some additional responsibilities to an object. In addition, the decoration mode is more flexible than generating subclass. [DP]

Figure 1 decorative pattern class diagram

Component is an object interface that can dynamically add responsibilities to these objects. Concretecomponent defines a specific object. You can also add some responsibilities to this object. Decorator is a decorative abstract class that inherits component and extends the functionality of the component class from the external class. However, for component, you do not need to know the existence of the decorator. As for concretedecorator, It is a specific decoration object and serves to add responsibilities to component. [DPE]

You can construct a concretedecorator object or call a function similar to setcomponent to pass in a reference to a specific concretecomponent object to encapsulate a specific concretecomponent object. Each decoration object only cares about its own functions, and does not need to care about how it is added to the object chain [DPE ].

I have seen most of the explanations about the decorative pattern in terms of the application appearance, so there is an "Object chain ", this implementation is to concatenate the specific decoration objects concretedecorator, and they all act on the specific objects of concretecomponent at the same time. However, there is another application that can "Parallel" the specific decoration objects such as concretedecorator. The specific decoration is called only when a function is required to act on the specific concretecomponent objects, this mode may be suitable for logic processing.

For some ideas, if there is only one concretecomponent class without an abstract component class, then the decorator class can be a subclass of concretecomponent (2 ). The author does not agree with this viewpoint. As Scott Meyers said, for inheritance, the relationship between subclass and parent class must satisfy the "is-a" relationship, the relationship between "decorator is a concretecomponent" is largely invalid. Therefore, the author thinks that the existence of the component abstract class is still necessary (3 ). However, if there is only one concretedecorator class, there is no need to create a separate decorator class. You can combine the responsibilities of decorator and concretedecorator into one class.

Figure 2 Human dress pictures 1

Figure 3 Human dress 2

In short, the decoration mode is a way to dynamically add more functions to existing functions. The advantage is that the decoration function in the class is removed from the class, which can simplify the original class and effectively separate the core responsibilities of the class from the decorative functional area, in addition, duplicate decoration logic in the related classes can be removed (when "in series ). The decorative mode also has its disadvantages. in system design, many small objects that look similar are often generated, making the system hard to understand and making troubleshooting more difficult.

For some of the advantages and disadvantages of the decorative pattern and their precautions, refer to the book "Design Pattern" (p117.

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.