This is the sixth day of my participation in the Gwen Challenge.More article challenges

This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

This article has participated in the weekend study program, click to see more details

How time flies! I thought I could finish learning all the design modes in Spring Boot over the weekend, but I didn’t expect that the ideal is full, while the reality is a little thin. Now I have understood the general design mode, but I still have a small part to go. Live on, look at the strategy pattern today

Strategy Pattern Strategy Pattern

Intent: Define a set of algorithms, encapsulate them one by one, and make them interchangeable.

What are strategic patterns?

Strategy refers to stratagem; Counsel. Generally, it refers to: 1. The set of schemes that can achieve the goal; 2. The course of action and methods of struggle formulated according to the development of the situation; 3. Have the art of fighting and can pay attention to ways and means. (here is an explanation of the word “strategy” from a department 🙂

Set of options for achieving goals: I think this explanation can also be applied to strategic patterns, such as: set of multiple algorithms for calculators. When traveling, the choice of various modes of travel (walking, cycling, car riding, or a visual tour of Mars via media following Tianwen 1) can also be applied

Scenario implementation

When a store sends a shirt to Xiao Ming or Xiao Xue, the choice of logistics is also a strategy. The first thing the store should consider is the freight cost of logistics. Borrow the last article (First introduction to Spring Cloud series – Spring Boot 05 | Java Development Practice) of the figure

The following code, to look at the choice of various logistics costs strategy

public class StrategyPatternDemo {

    public static void main(String[] args) {
        Context context = new Context(new Sf_express());
        System.out.println("Use SF Express freight" + context.executeStrategy(0.5) + "Yuan");

        context = new Context(new Jingdong_express());
        System.out.println("Use JD Express freight" + context.executeStrategy(0.5) + "Yuan");

        context = new Context(new Correo_express());
        System.out.println("Use YZ express freight" + context.executeStrategy(0.5) + "Yuan"); }}// Create logistics cost interface
interface Strategy {
    public double cost(double weight);
}
// Realization of specific logistics cost calculation
class Sf_express implements Strategy{
    @Override
    public double cost(double weight) {
        return 12 + weight*2; }}// Realization of specific logistics cost calculation
class Jingdong_express implements Strategy{
    @Override
    public double cost(double weight) {
        return 6 + weight*2; }}// Realization of specific logistics cost calculation
class Correo_express implements Strategy{
    @Override
    public double cost(double weight) {
        return 6 + weight*1; }}// Create a class Context that uses some policy
class Context {
    private Strategy strategy;

    public Context(Strategy strategy){
        this.strategy = strategy;
    }

    public double executeStrategy(double weight){
        return strategy.cost(weight);
    }

Copy the code

The results are as follows:

Application of policy patterns in Spring

In Spring, the Strategy pattern is used when instantiating objects

InstantiationStrategy as abstract interface SimpleInstantiationStrategy implementing an interface, but in methods instantiate judgment, for no MethodOverrides in bd, Constructor calls are made directly through JDK reflection

In view of the necessary methods against doing MethodOverrides, will be another way to deal with, in the SimpleInstantiationStrategy is through: InstantiateWithMethodInjection () method, in CglibSubclassingInstantiationStrategy do override the realization of the method.

Today’s summary

With the policy pattern done, I can learn a few things, from simple application scenarios, to implementation of scenarios, and the use of policy patterns in Spring to instantiate objects, but the instantiation process is still a bit vague and needs to be improved.