In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the process analysis of Springboot eliminating switch-case". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Background
Recently, when using springboot to develop an interface, you need to perform different operations and return different results according to the type of request event received. 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.
This is the end of the content of "Springboot process Analysis of eliminating switch-case". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.