In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you what the practical routine of SpringBoot combined with strategy model is, the content is concise and easy to understand, and it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
1.1. Preface
We all know that good design patterns can make our code more readable, extensible, and easy to maintain, but most programmers have learned design patterns at least once, and I don't know how to use them in practice. Next, let me introduce a strategy pattern combined with SpringBoot to make your code less if-else.
1.2. Start playing.
Don't say much nonsense, just tell you that the core of today is @ autowired. Are you familiar with this? you use it every day, isn't it automatically injected into Bean managed by Spring? But our use of it is often limited to global variable injection, forgetting that it can also be constructor injection, type injection or naming injection, so what kind of spark will the strategy pattern bloom? Follow my code.
1.2.1. Computing policy interface
/ * * @ author laoliangliang * @ date 10:10 on 2019-10-28 * / public interface CalculateStrategy {int doOperation (int num1,int num2);}
1.2.2. Implementation class
By implementing the three operations of addition, subtraction and multiplication respectively, you can see that I used the annotation @ Component of spring, that is, the instance is managed by spring.
@ Componentpublic class AddOperation implements CalculateStrategy {@ Override public int doOperation (int num1, int num2) {return num1 + num2;}}
@ Componentpublic class SubstractOperation implements CalculateStrategy {@ Override public int doOperation (int num1, int num2) {return num1-num2;}}
@ Componentpublic class MultiplyOperation implements CalculateStrategy {@ Override public int doOperation (int num1, int num2) {return num1 * num2;}}
1.2.3. Context
Context management is then created to extract the policy. This context is the focus of this article. Did you notice the location of the @ autowired note and the corresponding parameter list? In fact, it can also be injected into Map and List,Map, where key is the name it was injected, and List is where all instance objects are stored.
Import com.google.common.base.Preconditions;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import java.util.List;import java.util.Map;import java.util.concurrent.ConcurrentHashMap;/** * @ author laoliangliang * @ date 10:14 on 2019-10-28 * / @ Componentpublic class CalculatelOperationContext {/ / @ Autowired// private Map strategyMap; private final Map strategyMap = new ConcurrentHashMap () @ Autowired public void stragegyInteface (Map strategyMap) {this.strategyMap.clear (); strategyMap.forEach (this.strategyMap::put); System.out.println (this.strategyMap);} @ Autowired public void stragegyInteface2 (List strategyMap) {strategyMap.forEach (System.out::println);} public CalculateStrategy strategySelect (String mode) {Preconditions.checkArgument (! StringUtils.isEmpty (mode), "empty string is not allowed"); return this.strategyMap.get (mode);}}
Print the results:
{multiplyOperation=com.laoliang.springboot.pattern.strategy.MultiplyOperation@372ea2bc, addOperation=com.laoliang.springboot.pattern.strategy.AddOperation@4cc76301, substractOperation=com.laoliang.springboot.pattern.strategy.SubstractOperation@2f08c4b} com.laoliang.springboot.pattern.strategy.AddOperation@4cc76301com.laoliang.springboot.pattern.strategy.MultiplyOperation@372ea2bccom.laoliang.springboot.pattern.strategy.SubstractOperation@2f08c4b
You can see the relationship of key,value in Map. The default value of key is lowercase of the first letter of the class.
1.2.4. Control layer
/ * @ author laoliangliang * @ date, 2019-10-28 10:52 * / @ RestControllerpublic class StrategyController {@ Autowired private CalculatelOperationContext calculatelOperationContext; @ RequestMapping (value = "/ operation") public String strategySelect (@ RequestParam ("mode") String mode) {return String.valueOf (calculatelOperationContext.strategySelect (mode) .doOperation (20,5));}}
Start SpringBoot, the browser calls http://localhost:8080/operation?mode=multiplyOperation, and the result is 100. The other two addOperation and substractOperation modes can be selected.
I will demonstrate here that the input parameters are fixed. We can see that we call its method strategySelect through the context calculatelOperationContext and obtain different policies through different call parameters, so any method that can be abstracted in the business can be rewritten into such a pattern.
The advantage of this pattern routine is that when you want to add a strategy, such as division, you do not need to modify the original code, as long as the abstraction remains unchanged, you add a DivideOperation class to implement the CalculateStrategy policy interface plus Spring annotations, and when the pattern is changed to pideOperation, the call can be realized, the coupling is greatly reduced, and there is no need to change the original code that you can't look at.
You can see that the amount of code in the full text is still relatively small, different strategies are implemented with different classes, and there is no need to change other code. Have you get a new routine in this article?
What is the above content? what is the practical routine of SpringBoot combined with strategy model? have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.