Springboot消除switch-case的過程解析

本篇內(nèi)容介紹了“Springboot消除switch-case的過程解析”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),貢嘎企業(yè)網(wǎng)站建設(shè),貢嘎品牌網(wǎng)站建設(shè),網(wǎng)站定制,貢嘎網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,貢嘎網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

背景

最近,在使用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ù)也都集中在一個類當中,不好維護。因此,通過搜索學(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工具類

@Componentpublic 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)各種事件處理對象

@Componentpublic 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接口即可。

“Springboot消除switch-case的過程解析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

分享題目:Springboot消除switch-case的過程解析
鏈接地址:http://bm7419.com/article20/gocsjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、外貿(mào)建站、服務(wù)器托管、網(wǎng)站改版、虛擬主機、品牌網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計