本篇文章給大家分享的是有關(guān)Springboot中怎么消除switch-case,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

清河網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),清河網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為清河成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的清河做網(wǎng)站的公司定做!
基本邏輯如下:
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è)類當(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í)間處理類都要實(shí)現(xiàn)這個(gè)接口。其中,execute是事件的處理方法
編寫(xiě)具體的時(shí)間處理類
接下來(lái),逐個(gè)的編寫(xiě)事件處理類,舉下面一個(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;
}
}定義事件處理類工廠,用來(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接口即可。
以上就是Springboot中怎么消除switch-case,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站名稱:Springboot中怎么消除switch-case
網(wǎng)站URL:http://chinadenli.net/article10/pgpsgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、網(wǎng)站改版、網(wǎng)站收錄、用戶體驗(yàn)、動(dòng)態(tài)網(wǎng)站、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)