Decorative mode (Decorator)

Source: Internet
Author: User

Objective:
Decoration mode is also known as the packaging mode. Adornment mode extends the functionality of the object transparently to the client, in other words, the client does not feel that the object is different before and after decorating. Adornment mode extends objects without the use of creating more subclasses. is an alternative to an inheritance relationship.
(a) A brief picture of the decorative pattern:

(ii) Environmental role of decorative mode:
1. Abstract component (Component) role: An abstract interface is given to standardize the objects that are prepared to accept additional responsibilities.
2, concrete component (concrete Component) Role: Defines a class that will receive additional responsibilities.
3, decoration (Decorator) Role: Holds an instance of a component (Component) object and defines an interface that is consistent with the abstract interface.
4, the specific decorative role (concrete Decorator): Responsible for the Component object "affixed" additional responsibility.
(ii) sample source code:
Abstract component (Component) role:

package com.decorator;publicinterface Component{    /**     * 商业方法     */    void sampleOperation();}

Decoration (Decorator) Role:

 PackageCom.decorator; Public  class Decorator implements Component{    PrivateComponent Component;/** * Construction Sub * *     Public Decorator(Component Component) { This. component=component; }/** * Construction Sub * *     Public Decorator()    {//write your here;}/** * Business method, delegated to Component * /     Public void sampleoperation() {component.sampleoperation (); }}

Although it is not an abstract class, its function is an abstract role.
Specific component (concrete Component) Role:

package com.decorator;publicclass ConcreteComponent implements Component{    /**     * 构造子     */    publicConcreteComponent()    {        //write your code here    }    /**     * 商业方法     */    @Override    publicvoidsampleOperation()    {        // TODO Auto-generated method stub    }}

Specific decorative characters (concrete Decorator):

package  Com.decorator;public  class  concretedecorator  extends   Decorator  { /** * Business method */ public
      void  sampleoperation  () {super . sampleoperation (); }}

(iii) The relationship between decorative pattern and strategic mode
The decorative pattern replaces the epidermis of a thing and keeps it inside. The policy mode is the opposite, it is the specific algorithm can be interchangeable in the case of keeping the interface unchanged.
The implementation of the decoration mode requires the component class to be as "light" as possible, while the strategy mode requires the abstract policy class to be as "heavy" as possible.
(d) When to use decorative mode:
(1) Need to extend the functionality of a class or add additional responsibilities to a class
(2) The need to dynamically add functionality to an object, these functions can be dynamically revoked.
(3) The need to add some of the basic functions of the permutation of a very large number of functions, resulting in an inheritance relationship becomes unrealistic.
(v) Advantages and disadvantages of decoration mode:
Advantages of Decorative mode:
(1) The purpose of the adornment mode and the inheritance relation is to extend the function of the object, but the adornment mode can provide more flexibility than the inheritance. Decoration mode allows the system to dynamically decide to "paste" a desired "decoration", or to remove an unwanted "decoration". The inheritance relation is different, the inheritance relation is static, it decides before the system runs.
(2) Designers can create combinations of many different behaviors by using different decorative categories and combinations of these decorations.
Disadvantages of decorating mode:
Because of the use of adornment mode, a smaller number of destination classes are required than using inheritance relationships. The use of fewer classes, of course, makes the design easier to carry out. However, on the other hand, using the adornment pattern produces more objects than using the inheritance relationship. More objects make it difficult to find errors, especially when they look alike.
Reference: Java and patterns
"Head First design mode"
  

Decorative mode (Decorator)

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.