9 - Design Patterns - Decorator Pattern

Decorator Pattern Concept

The following is excerpted from the rookie tutorial

Intent: To dynamically add some additional responsibilities to an object. The decorator pattern is more flexible than subclassing in terms of adding functionality.

Main solution: Generally, in order to extend a class, we often use inheritance to implement it. Since inheritance introduces static features into the class, and with the increase of extended functions, subclasses will be very inflated.

When to use: Extending a class without adding many subclasses.

How to solve it: Divide specific functional responsibilities and inherit the decorator pattern at the same time.

Key code:
1. The Component class acts as an abstract role and should not be implemented concretely.
2. The modified class references and inherits the Component class, and the specific extension class overrides the parent class method.

Application examples:
1. Monkey King has 72 changes. When he becomes a "temple", he is still a monkey at the root, but he has the function of a temple.
2. A painting can be hung on the wall whether it is framed or not, but it is usually framed and actually the frame is hung on the wall. Before hanging on the wall, the picture can be covered with glass and framed; at this point the picture, the glass and the picture frame form one object.

Advantages:
The decorator class and the decorated class can develop independently and will not be coupled with each other. The decorator pattern is an alternative pattern to inheritance. The decorator pattern can dynamically extend the function of an implementation class.

Disadvantages: Multi-layer decoration is more complicated.

Usage scenarios:
1. Extend the functionality of a class.
2. Dynamically add functions and dynamically cancel.

Note: Can be used instead of inheritance.

A decorator, as the name suggests, is to add properties or methods to our component that it doesn't have. It consists of four parts, namely:
Component (abstract component): abstract interface that needs to be decorated
ConcreteComponent (concrete component): class to be decorated
Decorator (abstract decoration): parent class of decoration class
ConcreteDecorator (concrete decoration class): concrete Decorate

case

It is known that there is a cat that has a method of meowing, and now it is necessary to add a tree-climbing method to it through the decorator pattern.

Component

Abstract component, here refers to animal interface, with getAnimalProperty() method

public interface Animal {

    /**
     * 描述动物特征
     * @return void
     * 时间:2018年4月25日
     */
    public void getAnimalProperty();
}

ConcreteConponent

Specific components, here refers to animal cats

public class Cat implements Animal{

    @Override
    public void getAnimalProperty() {
        System.out.println("叫声:喵~");
    }

}

Decorator

Decorator class, here refers to the tree climbing decorator

public abstract class ClimbTreeDecorator implements Animal{

    private Animal animal;

    public ClimbTreeDecorator(Animal animal) {
        this.animal = animal;
    }

    @Override
    public void getAnimalProperty() {
        animal.getAnimalProperty();
    }

    /**
     * 爬树特征
     * @return void
     * 时间:2018年4月25日
     */
    public abstract void climbTree();
}

ConcreteDecrator

The specific decorator class, which is mainly responsible for the implementation of the tree climbing feature

public class ConcreteClimbDecorator extends ClimbTreeDecorator{

    public ConcreteClimbDecorator(Animal animal) {
        super(animal);
    }

    @Override
    public void climbTree() {
        System.out.println("爬树");
    }

    @Override
    public void getAnimalProperty() {
        climbTree();
        super.getAnimalProperty();
    }

}

test class

public class Test {
    public static void main(String[] args) {
        Animal cat = new Cat();
        Animal animal = new ConcreteClimbDecorator(cat);
        animal.getAnimalProperty();
    }
}

Summarize

At this point, the decorator pattern is introduced.
In fact, the decorator pattern is very simple. We first understand the concept of the decorator pattern. It is mainly responsible for adding additional properties or features to an existing object. Here, we call the addition a decoration.
And how do we decorate an existing object?

  • First abstract the object and create its parent interface
  • The object that needs to be decorated implements this parent class interface
  • Secondly, we abstract the decoration class. The abstract decoration class also implements the parent class interface. At the same time, through the combination method, the object to be decorated is injected, and the properties or methods that need to be decorated are created.
  • Next, we create a concrete decoration class and inherit this abstract decoration class, then it also has the parameterized construction method and abstract decoration method of the parent class, rewrite it, and then call it.

in addition

Here we compare the decorator pattern and the adapter pattern. The
decorator pattern is to add new properties/methods to an object without affecting its original properties/methods
, while the adapter pattern is to install the existing properties/methods in an object into Replace with the properties/methods we need

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325527043&siteId=291194637