In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use SpringBoot to achieve the strategy model, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
The raising of a question
The most annoying thing to encounter when reading other people's code is large chunks of if-else branch statements. Generally speaking, when you read below, you forget what the above is judging. A lot of materials talk about the use of policy patterns to improve this code logic.
The class diagram of the policy pattern is as follows:
Just write the code according to this diagram.
Implementation of Policy pattern Code
With the help of the Spring framework, we can easily implement the policy pattern.
To take a simple example, when we go to a coffee shop to buy coffee, we choose the size of the cup according to our preferences and stomach capacity. Then we need to implement a CoffeeStategy:
Package com.example.demo.strategy;public interface CoffeeStrategy {void offer ();}
Then there are the implementation of various specific strategies, taking medium coffee as an example:
Package com.example.demo.strategy;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;@Component ("MID") @ Slf4jpublic class MidCoffee implements CoffeeStrategy {@ Override public void offer () {log.info ("your medium coffee");}
Use the Component annotation to name the class MID, which works in a later application context. Start defining the application context class now:
Package com.example.demo.strategy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.Map;@Servicepublic class CoffeeContext {@ Autowired private Map coffeeStrategyMap; public void getCoffee (String size) {this.coffeeStrategyMap.get (size) .offer ();}
Because the Spring framework is used, all Bean is managed by Spring itself, and after startup, there are two elements in the Map: {"MID": MidCoffee} and {"LARGE": LargeCoffee}. In the specific business logic, you only need to introduce the application context class and use the getCoffee method each time.
For example, this Controller method:
@ GetMapping ("/ get") public void getCoffee (@ Param ("size") String size) {this.coffeeContext.getCoffee (size);}
If you request this API, we can see the specific log content in the background:
2021-09-30 22 com.example.demo.strategy.LargeCoffee 46 INFO 32.550 15628-[nio-8099-exec-1] com.example.demo.strategy.LargeCoffee: your large cup of coffee
2021-09-30 22 com.example.demo.strategy.LargeCoffee 46 com.example.demo.strategy.LargeCoffee 39.201 INFO 15628-[nio-8099-exec-7] coffee: your large cup of coffee
Further thinking
It has been written before that the name given in Component works wonders. If we don't use the Spring framework to implement the policy pattern, how do we write our code?
First of all, it is certain that policy interfaces and policy implementation classes do not need to change. What needs to change is the application context, because there is no automatic injection. This code will look something like this:
Package com.example.demo.strategy;public class CoffeeContext {CoffeeStrategy coffeeStrategy; public CoffeeContext (CoffeeStrategy coffeeStrategy) {this.coffeeStrategy = coffeeStrategy;} public void getCoffee () {this.coffeeStrategy.offer ();}}
In this way, in the actual use, I need to first create a specific implementation class object, and then pass this object into the policy application context. This approach does not seem to be as elegant as the implementation of Spring.
CoffeeStrategy mid = new MidCoffee (); CoffeeContext context = new CoffeeContext (mid); context.getCoffee ()
The advantage of this approach is that the same logic can be extracted into interface, and as long as the implementation class is not overridden, the default method is used by default.
After this transformation, my code has been streamlined a lot.
Thank you for reading this article carefully. I hope the article "how to implement the strategic model with SpringBoot" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.