Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the opening and closing principle in Springboot

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to realize the opening and closing principle in Springboot. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Without knowing the tedious design patterns, Springboot can help us to realize the opening and closing principle simply and conveniently.

If an object has multiple states (states are likely to increase), each state corresponds to a processing method, if if is used. Else... Each time a state is added, the original code needs to be changed, and the business that has moved the code needs to be retested in principle, which increases the burden on development, maintenance and testing. Springboot has a very useful feature, which makes it easy to get a collection of classes that implement an interface or inherit a class.

Define your own interface or abstract class first

Public interface MyInterface {String m ();}

Define classes that specifically implement methods in the interface

@ Service ("prevent duplicate prefixes _ status codes") public class MyClassA implements MyInterface {@ Override public String m () {/ / A's business return "A";}} @ Service ("prevent duplicate prefixes _ status codes") public class MyClassB implements MyInterface {@ Override public String m () {/ / B's business return "B";}}

Use

@ RestControllerpublic class MyController {@ Autowired Map myInterfaceMap; @ GetMapping ("/ api/ {status}") public String testStatus (@ PathVariable String status) {return myInterfaceMap.get ("prevent repetition prefix _" + status). M ();} about how to implement the opening and closing principle in Springboot. I hope the above can be helpful and learn more. 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report