In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of SpringBoot combined with strategy pattern, which is very detailed and has certain reference value. Friends who are interested must finish it!
1. SpringBoot combines strategy mode with actual combat routine 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. The computing policy interface / * * @ author laoliangliang * @ date on 2019-10-28 10:10 * / 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 the same, you add a DivideOperation class to implement the CalculateStrategy policy interface plus Spring annotations, and you can call it when the pattern is changed to divideOperation.
The above is all the content of the article "sample Analysis of SpringBoot combined with Strategy pattern". Thank you for reading! Hope to share the content to help you, more related knowledge, 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.