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ù)也都集中在一個類當(dāng)中,不好維護。因此,通過搜索學(xué)習(xí)發(fā)現(xiàn),可以使用Springboot的注解+策略模式+簡單工廠的方式來消除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;
}
}
定義一個注解
@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{
@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工具類
@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;
}
}
定義事件處理類工廠,用來生產(chǎn)各種事件處理對象
@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;
這樣,代碼就沒有了switch-case,增加一個事件也很簡單,只需要實現(xiàn)EventProcess接口即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot設(shè)置首頁(默認頁)跳轉(zhuǎn)功能的實現(xiàn)方案
- spring-boot @Component和@Bean的區(qū)別詳解
- SpringBoot2.0 整合 Dubbo框架實現(xiàn)RPC服務(wù)遠程調(diào)用方法
- SpringBoot+Dubbo+Seata分布式事務(wù)實戰(zhàn)詳解
- 使用springboot結(jié)合vue實現(xiàn)sso單點登錄
- eclipse怎么引入spring boot項目插件的方法
- 詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事件
- 詳解SpringBoot下文件上傳與下載的實現(xiàn)
相關(guān)文章
mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解
這篇文章主要給大家介紹了關(guān)于mybatis如何使用Java8的日期LocalDate和LocalDateTime的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
在本地用idea連接虛擬機上的hbase集群的實現(xiàn)代碼
這篇文章主要介紹了在本地用idea連接虛擬機上的hbase集群的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決
這篇文章主要介紹了springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring中BeanFactory與FactoryBean接口的區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于Spring中BeanFactory與FactoryBean接口的區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
利用Java的Struts框架實現(xiàn)電子郵件發(fā)送功能
這篇文章主要介紹了利用Java的Struts框架實現(xiàn)電子郵件發(fā)送功能,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下2015-12-12
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見的HTTP請求數(shù)據(jù)格式,它將請求參數(shù)編碼為鍵值對的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05

