“This is the 29th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”
This is the sixth article on design patterns, in which all the design patterns I will encounter are summarized one by one. I will write down my thoughts on each design pattern and how we can flexibly apply these design patterns in practical work. Welcome your attention. In this article we will look at extending the decorator pattern of a class in a more elegant way.
A brief introduction to the decorator pattern
The decorator pattern is a design pattern that dynamically extends a class without changing its structure.
Here we can regard the original category as blank housing, decorated category is commodity housing.
Class diagram for decorator pattern:
Various characters in decorator mode:
- Abstract components: Here we can think of them as raw materials.
- ConcreteComponent: Here we can think of it as a concrete concrete building.
- Decorator: Here we can think of it as commercial housing.
- Concretedecorators: Here we can think of concretedecorators as commercial houses with concrete designs.
The concrete realization idea of decorator pattern
- Create abstract artifacts.
- Create a concrete implementation of the abstract artifact.
- Create abstract decorators.
- Create concrete decorators.
The concrete implementation scheme of decorator pattern
Public abstract class Component{public abstract void operation(); } Public class ConcreteComponent extends Component{@override public void operation(){// Do something... Public abstract class extends Component{protected Component Component; public Decorator(Component component){ this.component = component; } @Override public void operation(){ this.component.operation(); }} Public Class ConcreteDecorator extends Decorator{private Object addField; public ConcreteDecorator(Component component){ super(component); } private void addMethod(){// do somethind... } private void decorateMethod(){// do somethind... } // Override the parent method public void operation(){this.decoratemethod (); super.operation(); }}Copy the code
Advantages and disadvantages of the decorator pattern
advantages
- Extending a class through decorators is more flexible than extending a class through inheritance.
- The decorative class and the decorated class are independent and the coupling degree is low.
disadvantages
- No clear inheritance structure;
- It is difficult to understand and troubleshoot problems when there are too many wrapping layers.
Application scenarios for the decorator pattern
- Need to dynamically extend the functionality of the object;
- Unable or inconvenient to extend functionality by subclass instead of inheritance;
- It can be used to restrict the execution conditions of objects, and also to control and check parameters.
Decorator pattern summary
After learning the decorator mode, I think the most confusing thing is the difference between the decorator mode and the proxy mode. Both of them allow me to enhance and extend the functions of the class. So how do we distinguish the proxy pattern from the decorator pattern? The proxy mode is the creation mode. Essentially, the proxy class is created when the class is created, and the methods are enhanced when the class is created. The decorator mode is a structural mode. We create objects and create original objects. For example, when we buy a house and buy a blank house, we need to decorate it when we move in.