The decorator pattern is a structural pattern.

Definition of decorator pattern

Decorator can dynamically add additional functionality to an object (enhancements) compared to inheritance, decorator can enhance classes that do not support inheritance; It is more flexible than inheritance and does not require a large number of subclasses to be generated.

Composition of decorator patterns

The class diagram for the decorator pattern looks like this:

Contains the following roles:

  • Component Abstract roles: Real and decorator objects have the same interface. In this way, the client object can interact with the decorator object in the same way as the real object.
  • ConcreteComponent Concrete Role: Define a concrete object, or add responsibilities to this object.
  • Decorator Decorator role: Holds a reference to an abstract artifact. The decorator object accepts all client requests and forwards these requests to the real object. In this way, new functionality can be added before and after the real object is called.
  • ConcreteDecorator ConcreteDecorator role: responsible for adding new responsibilities to component objects. Target Indicates the matched interface. Adaptee and Target interfaces are adapted.

Implementation of the decorator pattern

Abstract component

public abstract class House {  
    String description = "";  
    public String getDescription() {  
        return description;  
    }  
    public abstract void sleep();  
}Copy the code

The specific components

Public class MyHouse extends House {public MyHouse() {description = "MyHouse "; } @override public void sleep() {return "sleep in my own house "; }}Copy the code

Abstract decorator

public abstract class BigHouseDecorator extends House {  
    @Override  
    public abstract String getDescription();  
}Copy the code

Concrete decorator

public class European extends BigHouseDecorator { House house ; public European(House house) { this.house = house; } @override public String getDescription() {return house.getDescription(); } @override public void sleep() {return "sleep "; }}Copy the code

The test class

@Test
public void testDecorator() {
    House house = new MyHouse();  
    System.out.println(house.getDescription() + " : " + house.sleep());  
    house = new European(coffee);  
    System.out.println(coffee.getDescription()+" : " + house.sleep());  
}Copy the code

conclusion

This paper mainly introduces the concept and implementation of decorator pattern. In general, inheritance is often used to extend a class, because inheritance introduces static characteristics to the class and subclasses can swell as the number of extensions increases. To extend a class without adding many subclasses, you can use the decorator pattern.

The decorator pattern allows you to add functions to objects on the fly, adding functions to objects from outside an object and changing the appearance of the object. From the point of view of using the system externally, instead of using the original object, you use objects decorated with a series of decorators. This makes it possible to change the function of an object flexibly, as long as the dynamic composition of the decorator is changed, then the resulting object function has changed. Another benefit is the reuse of decorator functionality. You can add the same decorator to an object multiple times, or use the same decorator to decorate different objects. And it conforms to one of the basic rules of object-oriented design: “Use object composition, not object inheritance.”

Advantages are:

  • More flexible than inheritance. Inheritance is static, and once inherited all subclasses have the same functionality. In the decoration mode, functions are separated into each decorator and then dynamically combined at run time by object combination. The final functions of each decorated object are determined by the dynamic combination of functions at run time
  • Easier to reuse functionality. To facilitate reuse of decorator functions, you can add the same decorator to an object more than once, or use the same decorator to decorate different objects
  • Simplify high-level definitions. Decoration mode can add as many functions to an object as you want by combining decorators. Therefore, in high-level definition, only the most basic functions need to be defined, and corresponding decorators can be combined to complete the required functions when necessary

Disadvantages:

  • Multi-layer decoration is more complicated

reference

  1. Decorator mode
  2. [Design pattern]- Decorator pattern