您好,登錄后才能下訂單哦!
這篇文章主要介紹“Springboot中如何消除switch-case”,在日常操作中,相信很多人在Springboot中如何消除switch-case問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Springboot中如何消除switch-case”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
基本邏輯如下:
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: }
寫(xiě)完會(huì)發(fā)現(xiàn),隨著事件的增加,這段代碼會(huì)很長(zhǎng),每個(gè)事件的處理函數(shù)也都集中在一個(gè)類(lèi)當(dāng)中,不好維護(hù)。因此,通過(guò)搜索學(xué)習(xí)發(fā)現(xiàn),可以使用Springboot的注解+策略模式+簡(jiǎn)單工廠的方式來(lái)消除switch-case。
重構(gòu)
定義結(jié)構(gòu)體
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; } }
定義一個(gè)注解
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface CRSEventAnnotation { CRSEvent value(); }
定義事件處理接口
public interface EventProcess { CRSResponse execute(CRSRequest resquest); }
所有的時(shí)間處理類(lèi)都要實(shí)現(xiàn)這個(gè)接口。其中,execute是事件的處理方法
編寫(xiě)具體的時(shí)間處理類(lèi)
接下來(lái),逐個(gè)的編寫(xiě)事件處理類(lèi),舉下面一個(gè)例子:
@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("120"); response.setMessage(message); return response; } }
定義SpringContext工具類(lèi)
@Component public class SpringContextUtil implements ApplicationContextAware{ private ApplicationContext context; public ApplicationContext getContext(){ return context; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }
定義事件處理類(lèi)工廠,用來(lái)生產(chǎn)各種事件處理對(duì)象
@Component public class EventProcessFactory { @Autowired SpringContextUtil contextUtil; private static Map<CRSEvent, EventProcess> eventProcessMap = new ConcurrentHashMap<>(); public EventProcessFactory() { Map<String, Object> 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); } }
調(diào)用代碼修改
CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent()); EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent); if (eventProcess != null){ return eventProcess.execute(crsRequest); } return null;
這樣,代碼就沒(méi)有了switch-case,增加一個(gè)事件也很簡(jiǎn)單,只需要實(shí)現(xiàn)EventProcess接口即可。
到此,關(guān)于“Springboot中如何消除switch-case”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。