生活随笔
收集整理的這篇文章主要介紹了
Springboot消除switch-case方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Springboot消除switch-case方法
背景
最近,在使用springboot開發(fā)一個接口的時候,需要根據(jù)接收的請求事件類型,去執(zhí)行不同的操作,返回不同的結(jié)果,基本邏輯如下:
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:}
寫完會發(fā)現(xiàn),隨著事件的增加,這段代碼會很長,每個事件的處理函數(shù)也都集中在一個類當中,不好維護。因此,通過搜索學習發(fā)現(xiàn),可以使用Springboot的注解+策略模式+簡單工廠的方式來消除switch-case。
重構(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;}
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CRSEventAnnotation {CRSEvent value();
}
public interface EventProcess {CRSResponse execute(CRSRequest resquest);
}
所有的時間處理類都要實現(xiàn)這個接口。其中,execute是事件的處理方法
編寫具體的時間處理類
接下來,逐個的編寫事件處理類,舉下面一個例子:
@Component("appStartProcess")
@CRSEventAnnotation(value = CRSEvent.APP_START)
public class AppStartProcess implements EventProcess{@Overridepublic 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;}
}
@Component
public class SpringContextUtil implements ApplicationContextAware{private ApplicationContext context;public ApplicationContext getContext(){return context;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.context = applicationContext;}
}
- 定義事件處理類工廠,用來生產(chǎn)各種事件處理對象
@Component
public class EventProcessFactory {@AutowiredSpringContextUtil 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);}
}
CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent());EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent);if (eventProcess != null){return eventProcess.execute(crsRequest);}
return null;
這樣,代碼就沒有了switch-case,增加一個事件也很簡單,只需要實現(xiàn)EventProcess接口即可。
原博客
總結(jié)
以上是生活随笔為你收集整理的Springboot消除switch-case方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。