Java Strategy Pattern Explanation + Implementation

Java Strategy Pattern Explanation + Implementation

Strategy Pattern Overview

Strategy mode is a relatively common mode in the design and development mode of Java, and it plays an important role in actual development and application.
But in the actual development of many students, it is easy to do a lot of ifelse or switch and so on. Although it was very convenient when it was written at the time, subsequent maintenance is already very unfriendly to the cleanliness of the code.
Here first introduce the concept of the strategy pattern .
Strategy mode can be understood as a behavioral model, what to do under what circumstances, similar to ifelse, but it separates objects and behaviors, and performs conversion between behaviors through inheritance and interface calls, so that the implementation, that is, the algorithm, can be independent of user changes.
Compared with the crude ifelse, the strategy pattern divides the behavior and manages it through spring. It perfectly supports the "open and close principle", and the algorithm can be replaced or a new algorithm can be added without modifying the original system. In terms of application, there are many ifelse, and the implementation inside is different, and the strategy mode can be used when multiple code changes may be made to the behavior later.
Of course, because there are many strategy classes in the strategy mode, we must pay attention to the management of these strategy classes during management, otherwise it will bring a lot of trouble to find and understand the strategy classes later.

Realize the scene

After introducing the concepts and usage scenarios, let's explain an example to facilitate your understanding and use. After all, many students just want to find a demo that can be used directly on the road. So I will pull out a shelf for everyone here, and write some notes at the same time to facilitate everyone's reading and understanding.

Student A encountered a requirement during the development process. He needs to implement different solutions for four different products. Each solution is very complicated and may be changed and adjusted according to the market, and the support provided on the terminal is limited. At this time, he wants to use the strategy pattern to solve it.
First of all, the parameters on the end assign four products to "productA", "productB", "productC", and "productD" to distinguish them. Operate according to different categories. So he sets the abstract class strategy (similar to the top-level management class of the strategy):

public interface IBillStrategy {
    
    

    /**
     * 规则匹配的顶层接口,如果传出去的参数很多,String换成实体就好。括号里面的参数可以传一些需要实现和处理的参数
     *
     * @param dmpRuleSets
     * @param checkParam
     * @return
     */
    String ruleMarch(UpgradeRuleSetsEntity dmpRuleSets, CheckParam checkParam);
}

Then set different product strategies according to the four products:

@Service(“productA”)//把需要识别的类别放到这里,注意别导错包import org.springframework.stereotype.Service;
public class AllPassService implements IBillStrategy{
    
    
    @Override
    public String ruleMarch(UpgradeRuleSetsEntity dmpRuleSets, CheckParam checkParam){
    
    
    //里面存放相关的实现。参数什么的根据实际情况变化即可。
        System.out.println(“productA”);
        return “productA”;
    }
}

By analogy, set four (or more) policy classes. I won't go into details here.
Finally, you only need to add the following code where you need to judge or use.

IBillStrategy bean = applicationContext.getBean(“productA”, IBillStrategy.class);
        String ruleHitsParam = bean.ruleMarch(dmpRuleSets, checkParam);

Finally, student A got the processing result of the corresponding product.
The above is a simple implementation of the strategy pattern.

expand

For the strategy mode, there are many other implementation methods in Java, such as rule engines such as drools, which can match the rules one by one according to the parameters in the database, on the terminal or in the code. It is also a method for matching multiple rules. You can use it according to your own needs.

Guess you like

Origin blog.csdn.net/qq_43881656/article/details/127791018