In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to eliminate switch-case in Springboot". In daily operation, I believe many people have doubts about how to eliminate switch-case in Springboot. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to eliminate switch-case in Springboot"! Next, please follow the editor to study!
The basic logic is as follows:
String event = crsRequest.getEvent (); CRSResponse crsResponse = null; switch (event) {case CRSRequestEvent.APP_START: crsResponse = processAppStartCommand (crsRequest); break; case CRSRequestEvent.INIT_COMPLETE: crsResponse = processInitCompleteCommand (crsRequest); break; case CRSRequestEvent.COLLECT_COMPLETE: crsResponse = processCollectCompleteCommand (crsRequest); break; case CRSRequestEvent.COLLECT_NO_INPUT: crsResponse = processCollectNoInputCommand (crsRequest) Break; case CRSRequestEvent.PLAY_COMPLETE: crsResponse = processPlayCompleteCommand (crsRequest); break; default:}
After writing, you will find that as the number of events increases, this code will be very long, and the handling functions for each event will be concentrated in one class, which is difficult to maintain. Therefore, through search learning, it is found that Springboot's annotations + policy pattern + simple factory can be used to eliminate switch-case.
Reconstruct
Define the structure
Public enum CRSEvent {APP_START ("APP_START"), INIT_COMPLETE ("INIT_COMPLETE"), PLAY_COMPLETE ("PLAY_COMPLETE"), COLLECT_COMPLETE ("COLLECT_COMPLETE"), COLLECT_NO_INPUT ("COLLECT_NO_INPUT"), APP_END ("APP_END"), RESP_ERROR_CMD ("RESP_ERROR_CMD"); private String event; CRSEvent (String event) {this.event = event;} public String getEvent () {return event } public void setEvent (String event) {this.event = event;}}
Define an annotation
Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) public @ interface CRSEventAnnotation {CRSEvent value ();}
Define event handling interface
Public interface EventProcess {CRSResponse execute (CRSRequest resquest);}
All time processing classes implement this interface. Where execute is the event handling method
Write specific time processing classes
Next, write event handling classes one by one, giving the following example:
@ Component ("appStartProcess") @ CRSEventAnnotation (value = CRSEvent.APP_START) public class AppStartProcess implements EventProcess {@ Override public CRSResponse execute (CRSRequest resquest) {CRSResponse response = new CRSResponse (); response.setCommand (CRSResponseCmd.IVR_SESSION_INIT); CRSResponse.Message message = new CRSResponse.Message (); message.setTts_vid ("65580"); message.setTts_speed ("120s"); response.setMessage (message); return response;}}
Define SpringContext utility classes
@ Componentpublic class SpringContextUtil implements ApplicationContextAware {private ApplicationContext context; public ApplicationContext getContext () {return context;} @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this.context = applicationContext;}}
Define event handling class factories, which are used to produce various event handling objects
@ Componentpublic class EventProcessFactory {@ Autowired SpringContextUtil contextUtil; private static Map eventProcessMap = new ConcurrentHashMap (); public EventProcessFactory () {Map beanMap = contextUtil.getContext () .getBeansWithAnnotation (CRSEventAnnotation.class); for (Object evetProcess: beanMap.values ()) {CRSEventAnnotation annotation = evetProcess.getClass () .getAnnotation (CRSEventAnnotation.class); eventProcessMap.put (annotation.value (), (EventProcess) evetProcess);}} public static EventProcess createEventProcess (CRSEvent event) {return eventProcessMap.get (event);}}
Calling code modification
CRSEvent crsEvent = CRSEvent.valueOf (crsRequest.getEvent ()); EventProcess eventProcess = EventProcessFactory.createEventProcess (crsEvent); if (eventProcess! = null) {return eventProcess.execute (crsRequest);} return null
In this way, the code has no switch-case, and it's easy to add an event, just implement the EventProcess interface.
At this point, the study of "how to eliminate switch-case in Springboot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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