The correct posture used in the strategy mode in SPRING

The correct posture used in the strategy mode in SPRING

 

1. Introduction to Strategy Mode    

      Strategy mode: The strategy mode is a powerful tool that can replace a large number of if-else codes. There are many application scenarios: such as payment (WeChat payment, Alipay payment, bank opening payment), integrated e-commerce platform decryption (pdd decryption, dj decryption, Taobao) Decrypt), the strategy model provides an entrance to a unified solution to the outside world, and the specific solution strategy is chosen by yourself; the script we often hear is that a cto angry developer who writes if-else deducts a salary of 1000, which shows how high-level people hate if -else, so the strategic model is particularly important in reducing the coupling degree of project organization and reducing the maintenance cost of the project.

 

2. The use of strategy mode

     Let's choose one of the scenarios in the above introduction, for example, in a logistics project, integrating various e-commerce platforms and then decrypting the order recipient information.

     (1) Define the strategy interface

public interface IDecryptStrategyService {

    /**
     * 做解密操作
     *
     * @param order 订单数据
     */
    void doDecrypt(Order order);
}

  (2) The specific realization of the defined strategy

@Service
public class PddDecryptStrategyService implements IDecryptStrategyService {

    /**
     * 做解密操作
     *
     * @param order 订单信息
     */
    public void doDecrypt(OrderInfo order) {
       doSomthing();
    }
}
@Service
public class JdDecryptStrategyService implements IDecryptStrategyService {


    /**
     * 做解密操作
     *
     * @param order 订单信息
     */
    public void doDecrypt(OrderInfo order) {
       doSomthing(order);
    }
}

(4) Implementation of the implementation-defined strategy selector

@Service
public class DecryptStrategySelector {

    @Autowired
    private Map<String, IDecryptStrategyService> decryptStrategyServiceMap;

    public void doDecrypt(OrderInfo order) {
     // 获取策略名称map
     Map<String, String> strategyMap = Enums.PlantDecryptStrategyEnum.getMap();
     // 获取策略名称
     String strategyName = strategyMap.get(order.getPlantCode);
     // 获取策略
     IDecryptStrategyService decryptStrategyService = decryptStrategyServiceMap.get(strategyName);
     // 执行策略
     decryptStrategyService.doDecrypt(order);
    }
}


public class Enums {
 
 public enum PlantDecryptStrategyEnum {

        PDD("PDD", "pddDecryptStrategyService");
        JD("JD", "jdDecryptStrategyService");
        private String code;
        private String strategyName;

        PlantDecryptStrategyEnum(String code, String strategyName) {
            this.setCode(code);
            this.setStrategyName(strategyName);
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getStrategyName() {
            return strategyName;
        }

        public void setStrategyName(String strategyName) {
            this.strategyName = strategyName;
        }

        private static Map<String, String> map = new HashMap<>();

        static {
            map.put(PDD.code, PDD.strategyName);
        }

        public static Map<String, String> getMap() {
            return map;
        }
    }
}

  The above is how to use the strategy mode in spring

  3. Summary

   (1) The above strategy model cases are relatively uncomplicated, and the main logic is reflected in the decryption strategies of different platforms. The structure is relatively simple, and such design patterns are not often used in actual development. In addition, this design is similar to the command mode and the adapter mode structure, but the ideas are different.

   (2) The use of the strategy design pattern can optimize the if statement in our method. The use of a large number of if statements makes the code difficult to expand and difficult to maintain. At the same time, it is also very difficult to encounter various problems in the later stage. Difficult to maintain. After using such a design pattern, isolation and scalability can be met very well, and it is very easy to undertake the ever-increasing needs.

   (3) Strategy mode, adapter mode, combination mode, etc. are relatively similar in some structures, but each mode has its own logical characteristics. The best way to use it is through More practice to learn from experience and provide better technical output for subsequent R&D and design.
 

Guess you like

Origin blog.csdn.net/aazhzhu/article/details/113660759