Design Pattern 6 (decoration pattern)

Source: Internet
Author: User

Decoration mode: the dynamic topology adds some additional responsibilities to an object. In addition, the decoration mode is more flexible than the subclass generation.

 

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.

The Decorator decorative abstract class inherits the Component and extends the Componnt class functions from the external class. However, for Component, you do not need to know the existence of the Decorator.

Component class

        

ConcreteComponnet class

       

Decorator class

                                  .component =                     (component != 

ConcreteDecoratorA

                          = 

ConcreteDecoratprB

                          


Client call instance

       Main(= = = 

The decoration method is as follows: first, use ConcreteComponent to instantiate cc, then use ConcreteComponentA's instantiated object ca to wrap cc, then use ConcreteComponetB's object cb to package ca, and finally execute cb Operation ()

Setcomponent is actually used to wrap the object. In this way, the implementation of each decoration object is separated from how to use this object. Each decoration object only cares about its own functions and does not need to be added to the object chain.

 

However, there are also special cases: if there is only one ConcreteComponent class without an abstract Component class, then the Decorator class can be a subclass of ConcreteComponent. Similarly, if there is only one ConcreteDecorator class, there is no need to create a separate Decorator class. Instead, you can combine the responsibilities of Decorator and ConcreteDecorator into one class.

The decoration mode is a way to dynamically add more functions to existing functions.

The advantage is that the decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core responsibilities of the class from the decorative functional area.

 

Related Article

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.