Springboot消除switch-case過程解析
背景
最近,在使用springboot開發(fā)一個(gè)接口的時(shí)候,需要根據(jù)接收的請(qǐng)求事件類型,去執(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:
}
寫完會(huì)發(fā)現(xiàn),隨著事件的增加,這段代碼會(huì)很長,每個(gè)事件的處理函數(shù)也都集中在一個(gè)類當(dāng)中,不好維護(hù)。因此,通過搜索學(xué)習(xí)發(fā)現(xiàn),可以使用Springboot的注解+策略模式+簡(jiǎn)單工廠的方式來消除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í)間處理類都要實(shí)現(xiàn)這個(gè)接口。其中,execute是事件的處理方法
編寫具體的時(shí)間處理類
接下來,逐個(gè)的編寫事件處理類,舉下面一個(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工具類
@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)各種事件處理對(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;
這樣,代碼就沒有了switch-case,增加一個(gè)事件也很簡(jiǎn)單,只需要實(shí)現(xiàn)EventProcess接口即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot設(shè)置首頁(默認(rèn)頁)跳轉(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)聽ApplicationEvent事件
- 詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
相關(guān)文章
Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表
這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
簡(jiǎn)述Springboot @Async 異步方法
這篇文章主要介紹了Springboot @Async 異步方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05
Spring Boot Actuator監(jiān)控器配置及使用解析
這篇文章主要介紹了Spring Boot Actuator監(jiān)控器配置及使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決
這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06
Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式
這篇文章主要介紹了Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
win10 java(jdk安裝)環(huán)境變量配置和相關(guān)問題
這篇文章主要介紹了win10java(jdk安裝)環(huán)境變量配置和相關(guān)問題解決,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
關(guān)于Socket的解析以及雙方即時(shí)通訊的java實(shí)現(xiàn)方法
本篇文章主要介紹了關(guān)于Socket的解析以及雙方通訊的java實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03

