This is the 30th day of my participation in the More Text Challenge. For more details, see more text Challenge


👉 Design mode directory

What is a Decorator pattern?

concept

Decorator patterns are structural patterns that dynamically add new responsibilities (functions) to an object without changing the existing object structure (code), hoping to extend a class by combining objects instead of inheriting.

As we know from the name, decorators, like the watches that we wear, can do without affecting us, and then give us the ability to tell the time.

The decorator pattern is about adding new functionality without changing the core functionality. It is very similar to the agent pattern. I’ll summarize the differences after I learn how to implement the decorator pattern.

advantages

  1. The decorator pattern is an alternative to inheritance by combining the functions of inheritance without the intrusiveness of inheritance.
  2. Reduced coupling between classes. The decorated and decorated classes can be developed independently without affecting each other.
  3. It conforms to the principle of openness and closure.

disadvantages

Overuse of the decorator pattern can lead to a large number of classes and increase the complexity of a program.

Design patterns are good, but don’t drink too much.

The principle of

“+” means compliance, and “-” means noncompliance or irrelevant

The principle of Open the closed Single responsibility Di milt Replacement on the Richter scale Dependency inversion Interface segregation Synthesis of reuse
+ + + + + +

Applicable Scenario (Motivation)

  1. Want to avoid using inheritance to extend functionality.
  2. Implementation subclasses have different dimensions.

How to implement

To implement the decorator pattern, you need four things:

  1. Abstract component interface: Defines the interface of a concrete component class.
  2. Concrete component class (decorated class) : implements the abstract component interface, created for the class to be decorated.
  3. Decorative abstract classes: implement abstract components, specify the methods that the implementing class implements, and combine the objects that concretely construct the class.
  4. Concrete decorator class: inherits the decorator abstract class, realizes the method.

On the class diagram

In the code

Abstract Component interface: Component

/** ** Abstract Component interface * Simulate human * Created on 2021/6/2. **@author xuxiaobai
 */
public interface Component {

    void say(a);

    void eat(a);
}
Copy the code

ConcreteComponent class: ConcreteComponent

/** * Created on 2021/6/2. **@author xuxiaobai
 */
public class ConcreteComponent  implements Component{
    @Override
    public void say(a) {
        System.out.println("Hello, my name is Cui hua.");
    }

    @Override
    public void eat(a) {
        System.out.println("Yummy!"); }}Copy the code

Decorates abstract classes: Decorator

/** * Created abstract class * Created on 2021/6/2@author xuxiaobai
 */
public abstract class Decorator implements Component{
    Component component=new ConcreteComponent();

    @Override
    public void say(a) {
        component.say();
    }

    @Override
    public void eat(a) {
        component.eat();
    }

    /** * See the time */
    public abstract void lookTime(a);
}
Copy the code

ConcreteDecorator: ConcreteDecorator

/** * Created on 2021/6/2. **@author xuxiaobai
 */
public class ConcreteDecorator extends Decorator{

    @Override
    public void lookTime(a) {
        System.out.println("I see on my Mi Band, it's 09:30."); }}Copy the code

Testing:

/**
 * Created on 2021/6/2.
 *
 * @author xuxiaobai
 */
public class DecoratorTest {
    public static void main(String[] args) {
        /** * No watch cui hua class */
        Component component = new ConcreteComponent();
        component.say();
        component.eat();

        /** * Have watch cui hua class */
        Decorator decorator = new ConcreteDecorator();
        decorator.say();
        decorator.eat();
        decorator.lookTime();

        /** * Result: * Hello, my name is Cuihua. * Delicious! * Hello, my name is Cui Hua. * Delicious! * I see my Mi Bracelet says it's 09:30 */}}Copy the code

Here, Cui Hua wears a watch as a Decorator, or doesn’t wear a watch as a Decorator. It’s very flexible.

conclusion

Difference from proxy mode:

The decorator pattern is very similar to the static proxy pattern. The proxy pattern also uses static proxy to implement the proxy. What is the difference between the two?

Yes, the focus of the decorator pattern is to expand new ways, like the example above, cui Hua in addition to talking and eating, but also expanded the way to see time; The proxy pattern is for each of the original method of additional new features, such as cui flower to pack to KFC home to eat, can go to the store, can also point on KFC small program, equivalent to help you tell the clerk of small programs you order, do go to the store with his points do is the same, just don’t have to go to the store now.

In my opinion, although the decorator pattern is an alternative to inheritance, when the number of classes you want to extend is not very large, you can consider using inheritance to extend directly. When the number of extended classes becomes a little too large, you can refactor the stack of classes to reconstitute the decorator pattern.

— — — — — — — — — — — — — — —

The more you know, the more you don’t know.

If you have questions about this article, please comment directly or send me a personal message. If you think my writing is good, you can also support me by clicking “like”

Without permission, shall not be reproduced!