Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

An overview of the

Adornment mode, also known as decorator mode, decorator mode. A structural design pattern that dynamically adds additional behavior to an object by placing it in a characteristic wrapper that contains behavior.

Decoration mode is more flexible in terms of adding functionality than subclasses.

role

Abstract part: A common interface used to declare ornamented and decorated objects.

Concrete part: An interface that implements the abstract part, which defines the underlying behavior of the class to which the decorated object belongs.

Abstract decoration: This class has a reference member variable that points to the object being decorated. The type of the variable is defined as an abstract part, so that concrete parts and decorations can be referenced.

Concrete decoration: Defines additional behavior that can be dynamically added to the part, overriding the methods of the class being decorated.

The template

Abstract Part: Declares the interface that the car class needs to implement

public interface Car {
    /** ** car assembly */
    void show(a);
}
Copy the code

Concrete parts: the concrete implementation class of the automobile interface, which declares the concrete assembly of the Mercedes Benz

public class Benz implements Car{
    @Override
    public void show(a) {
        System.out.println("The default color of a Mercedes is black."); }}Copy the code

Abstract decoration: Defines traversal of abstract part types, introducing an object to be decorated

public abstract class CarDecorator {
    private Car car;

    public CarDecorator(Car car) {
        this.car = car;
    }

    public void show(a){
        this.car.show(); }}Copy the code

Specific decoration: to be decorated decoration

public class ConcreteCarDecorator extends CarDecorator{
    public ConcreteCarDecorator(Car car) {
        super(car);
    }

    /** * Paint the car */
    private void print(a) {
        System.out.println("Paint the word" Newbie "on the back of the car in purple glow.");
    }

    /** * Install GPS device */
    private void setGps(a) {
        System.out.println("Install GPS navigation system.");
    }

    /** * override method */
    @Override
    public void show(a) {
        super.show();
        this.print();
        this.setGps(); }}Copy the code

summary

advantages

Convenience: Is an alternative to inheritance that extends objects without creating new subclasses

Decoupling: The decorator class is independent of the decorator class, and the decorator class does not specify a decorator class, nor does the decorator class specify a specific part implementation

Dynamic: You can dynamically extend an implementation class to add or remove the functionality of decorated objects

disadvantages

Complex: The initial configuration code for each layer is complex

Difficulty: It is difficult to remove characteristic decorators from the decorator stack and difficult to implement decorators that are not affected by the order of the decorator stack

Applicable scenario

  • When you want to add additional functionality to a class without changing the code
  • You need to modify or add functionality to a batch of classes
  • befinalThe keyword modifier class is further extended

The last

The article has written the bad place, asks the big guy to be generous to give advice, the mistake is can let the person grow up most, wishes me and the big guy the distance to shorten gradually!!

If you feel the article is helpful to you, please click like, favorites, follow, comment four consecutive support, your support is the biggest power of my creation!!