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 > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces several implementation classes of spring automatic injection interface, the content is very detailed, interested friends can refer to, hope to be helpful to you.
When using spring development, sometimes there will be multiple implementation classes in one interface, but is there sometimes such a situation, that is, it is not clear which implementation class you need to use in your logic code, that is, if you go to massage, there are several discounts for members in massage parlors, such as vip, svip and ordinary users. Is it necessary to define different discounts for these members in massage parlors? Then calculate different consumption according to the different members of each user.
Although in such a situation, for ordinary people, the first glance must be to add if else directly to judge.
If you add another member to this massage parlor in the future, do you have to modify your logic code to add an if else? this violates the opening and closing principle of system architecture design, and writing if else in this way makes your code look unelegant.
So in the code, we can first define a DiscountStrategy interface class
Public interface DiscountStrategy {
Public String getType ()
Public double disCount (double fee)
}
And then write some of his implementation classes.
Ordinary user implementation class
@ Service
Public class NormalDisCountService implements DiscountStrategy {
Public String getType () {
Return "normal"
}
Public double disCount (double fee) {
Return fee * 1
}
}
Member realization class
Public class VipDisCountService implements DiscountStrategy {
Public String getType () {
Return "vip"
}
Public double disCount (double fee) {
Return fee * 0.8
}
}
Svip Super member implementation Class
@ Service
Public class SVipDisCountService implements DiscountStrategy {
Public String getType () {
Return "svip"
}
Public double disCount (double fee) {
Return fee * 0.5
}
}
Then when a user comes in to spend money, give a discount according to your current identity.
Define a map collection, then put all the implementation classes into this collection, and then perform different operations according to the current member type
@ Service
Public class DisCountStrageService {
Map discountStrategyMap = new HashMap ()
/ / constructor. If you are a collection interface object, all the subclasses about that interface in the spring container will be grabbed out and put into the collection.
Public DisCountStrageService (List discountStrategys) {
For (DiscountStrategy discountStrategy: discountStrategys) {
DiscountStrategyMap.put (discountStrategy.getType (), discountStrategy)
}
}
Public double disCount (String type,Double fee) {
DiscountStrategy discountStrategy = discountStrategyMap.get (type)
Return discountStrategy.disCount (fee)
}
}
Test class
@ RunWith (SpringRunner.class)
@ SpringBootTest
Public class MzySpringModeApplicationTests {
@ Autowired
OrderService orderService
@ Autowired
DisCountStrageService disCountStrageService
@ Test
Public void contextLoads () {
/ / orderService.saveOrder ()
Double vipresult = disCountStrageService.disCount ("vip", 100d)
Double svipresult = disCountStrageService.disCount ("svip", 100d)
Double normalresult = disCountStrageService.disCount ("normal", 100d)
System.out.println (vipresult)
System.out.println (svipresult)
System.out.println (normalresult)
}
}
In fact, this is the strategic pattern of the java design pattern, but it is injected into the list collection with the constructor.
Even if massage parlors continue to add a membership system, such as black card users, only need to write an interface implementation class, and do not need to modify the previous code, this does not violate the opening and closing principle of the system architecture (closed for modification, open for extension)
Note: the above method can only be applied to springboot projects. If the previous springmvc mode is not supported, an error will be reported, because the previous method injection is injected through the configuration file, and you need to modify an applicationContext configuration file.
On the spring automatic injection interface of multiple implementation classes are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.