Springboot消除switch-case過(guò)程解析
背景
最近,在使用springboot開(kāi)發(fā)一個(gè)接口的時(shí)候,需要根據(jù)接收的請(qǐng)求事件類(lèi)型,去執(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:
}
寫(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接口即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot設(shè)置首頁(yè)(默認(rèn)頁(yè))跳轉(zhuǎn)功能的實(shí)現(xiàn)方案
- spring-boot @Component和@Bean的區(qū)別詳解
- SpringBoot2.0 整合 Dubbo框架實(shí)現(xiàn)RPC服務(wù)遠(yuǎn)程調(diào)用方法
- SpringBoot+Dubbo+Seata分布式事務(wù)實(shí)戰(zhàn)詳解
- 使用springboot結(jié)合vue實(shí)現(xiàn)sso單點(diǎn)登錄
- eclipse怎么引入spring boot項(xiàng)目插件的方法
- 詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽(tīng)ApplicationEvent事件
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
相關(guān)文章
mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解
這篇文章主要給大家介紹了關(guān)于mybatis如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
在本地用idea連接虛擬機(jī)上的hbase集群的實(shí)現(xiàn)代碼
這篇文章主要介紹了在本地用idea連接虛擬機(jī)上的hbase集群的實(shí)現(xiàn)代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
springboot配置數(shù)據(jù)庫(kù)密碼特殊字符報(bào)錯(cuò)的解決
這篇文章主要介紹了springboot配置數(shù)據(jù)庫(kù)密碼特殊字符報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring中BeanFactory與FactoryBean接口的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Spring中BeanFactory與FactoryBean接口的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
利用Java的Struts框架實(shí)現(xiàn)電子郵件發(fā)送功能
這篇文章主要介紹了利用Java的Struts框架實(shí)現(xiàn)電子郵件發(fā)送功能,Struts框架是Java的SSH三大web開(kāi)發(fā)框架之一,需要的朋友可以參考下2015-12-12
Token登陸驗(yàn)證機(jī)制的原理及實(shí)現(xiàn)
這篇文章介紹了Token登陸驗(yàn)證機(jī)制的原理及實(shí)現(xiàn),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見(jiàn)的HTTP請(qǐng)求數(shù)據(jù)格式,它將請(qǐng)求參數(shù)編碼為鍵值對(duì)的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05
JVM 的 noverify 啟動(dòng)參數(shù)問(wèn)題解析
這篇文章主要介紹了JVM 的 noverify 啟動(dòng)參數(shù)問(wèn)題解析,從 JDK 13 開(kāi)始及其后續(xù)版本中,不建議繼續(xù)使用?-Xverify:none?和-noverify?參數(shù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05

