欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot消除switch-case過程解析

 更新時間:2019年10月16日 09:30:41   作者:巡山小妖N  
這篇文章主要介紹了Springboot消除switch-case過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

背景

最近,在使用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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • mybatis如何使用Java8的日期LocalDate和LocalDateTime詳解

    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)代碼

    這篇文章主要介紹了在本地用idea連接虛擬機上的hbase集群的實現(xiàn)代碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • Java中程序的運行全過程

    Java中程序的運行全過程

    這篇文章主要介紹了Java中程序的運行全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決

    springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決

    這篇文章主要介紹了springboot配置數(shù)據(jù)庫密碼特殊字符報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • JavaFX之TableView的使用詳解

    JavaFX之TableView的使用詳解

    這篇文章主要介紹了JavaFX之TableView的使用,有需要的朋友可以參考一下
    2013-12-12
  • Spring中BeanFactory與FactoryBean接口的區(qū)別詳解

    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ā)送功能

    這篇文章主要介紹了利用Java的Struts框架實現(xiàn)電子郵件發(fā)送功能,Struts框架是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Token登陸驗證機制的原理及實現(xiàn)

    Token登陸驗證機制的原理及實現(xiàn)

    這篇文章介紹了Token登陸驗證機制的原理及實現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • 后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)

    后端如何接收格式為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
  • JVM 的 noverify 啟動參數(shù)問題解析

    JVM 的 noverify 啟動參數(shù)問題解析

    這篇文章主要介紹了JVM 的 noverify 啟動參數(shù)問題解析,從 JDK 13 開始及其后續(xù)版本中,不建議繼續(xù)使用?-Xverify:none?和-noverify?參數(shù),本文給大家介紹的非常詳細,需要的朋友可以參考下
    2023-05-05

最新評論