Java Design Patterns-Detailed Explanation of Decorator Pattern

We will use the decoration mode during the development process. Let's study the decoration mode as a case study:

first

1. Advantages of decoration:
Decorating classes and decorated classes can develop independently without being coupled to each other. Decoration mode is an alternative mode to inheritance. Decoration mode can dynamically extend the functions of an implementation class.

2. Disadvantages
Multi-layer decoration is more complicated.

General scene mode:
a. Extend the functionality of a class.
b. Dynamically add functions and dynamically cancel them.

Example

Scene description: Suppose we usually need to walk to go to work, and we also need to walk when returning home from work. The common point is walking. The difference is that one is going home, and the other is that we design a decorative completion action for this scene at work.

1. Define an operation interface and implement classes to implement specific logic (go to work, go home)

/**
 * @author zhaoyy
 * @version 1.0
 * @description TODO
 * @date 2022/9/20
 **/
public interface OperationType {
    
    
    public String operation();
}

2 pairs of interface implementation classes:

/**
 * @author zhaoyy
 * @version 1.0
 * @description TODO
 * @date 2022/9/20
 **/
public class GoWorkOperationType implements OperationType{
    
    
    @Override
    public String operation() {
    
    
        return "go work";
    }
}


/**
 * @author zhaoyy
 * @version 1.0
 * @description TODO
 * @date 2022/9/20
 **/
public class GoHomeOperationType implements OperationType{
    
    
    @Override
    public String operation() {
    
    
        return "go home";
    }
}

3. Create an adaptation class to complete the entire process operation

/**
 * @author zhaoyy
 * @version 1.0
 * @description 装饰类
 * @date 2022/9/20
 **/
public class DecorateClass {
    
    
    private OperationType operationType;

    public DecorateClass(OperationType operationType) {
    
    
        this.operationType = operationType;
    }
    private String commonOperation(){
    
    
        return "走路";
    }

    public void process(){
    
    
        String operation = operationType.operation();
        System.out.println(operation);
        String common = this.commonOperation();
        System.out.println(common);
    }
}

Final test class:

public class Test {
    
    
    public static void main(String[] args) {
    
    
        DecorateClass goHome = new DecorateClass(new GoHomeOperationType());
        goHome.process();
        System.out.println("=========================================================");
        DecorateClass goWork = new DecorateClass(new GoWorkOperationType());
        goWork.process();
    }
}

Test Results:
Insert image description here

illustrate

As can be seen from the above example, you need to walk whether you are going home or to work. The code is relatively simple and I don’t need to explain too much. The most important thing is to share the design patterns of decoration classes. Everyone can design their own decoration classes according to their own business scenarios at work. , to process the corresponding business logic. Design advantages: the code structure is hierarchical, easy to expand, and easy to maintain and understand later.

Guess you like

Origin blog.csdn.net/weixin_43829047/article/details/126957714