最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot消除switch-case過程解析

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

背景

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

相關(guān)文章

  • Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表

    Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表

    這篇文章主要介紹了Springboot如何根據(jù)實(shí)體類生成數(shù)據(jù)庫表的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 簡(jiǎn)述Springboot @Async 異步方法

    簡(jiǎn)述Springboot @Async 異步方法

    這篇文章主要介紹了Springboot @Async 異步方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-05-05
  • Spring Boot Actuator監(jiān)控器配置及使用解析

    Spring Boot Actuator監(jiān)控器配置及使用解析

    這篇文章主要介紹了Spring Boot Actuator監(jiān)控器配置及使用解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Springboot3整合Mybatis3的完整步驟記錄

    Springboot3整合Mybatis3的完整步驟記錄

    這篇文章主要給大家介紹了關(guān)于Springboot3整合Mybatis3的完整步驟,Spring Boot和MyBatis分別是兩個(gè)功能強(qiáng)大的框架,它們的協(xié)同使用可以極大地簡(jiǎn)化數(shù)據(jù)訪問層的開發(fā),提高整體的開發(fā)效率,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • 詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決

    這篇文章主要介紹了詳解SpringBoot 多線程處理任務(wù) 無法@Autowired注入bean問題解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-06-06
  • java并發(fā)編程之cas詳解

    java并發(fā)編程之cas詳解

    這篇文章主要介紹了java并發(fā)編程之cas詳解,涉及cas使用場(chǎng)景和cas用作原子操作等內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式

    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)問題

    win10 java(jdk安裝)環(huán)境變量配置和相關(guān)問題

    這篇文章主要介紹了win10java(jdk安裝)環(huán)境變量配置和相關(guān)問題解決,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 詳解Java雙軸快速排序算法

    詳解Java雙軸快速排序算法

    在排序算法中,快速排序是占比非常多的一環(huán),但是快速排序其思想一直被考察研究,也有很多的優(yōu)化方案。這里主要講解雙軸快速排序的思想和實(shí)現(xiàn)
    2021-06-06
  • 關(guān)于Socket的解析以及雙方即時(shí)通訊的java實(shí)現(xiàn)方法

    關(guān)于Socket的解析以及雙方即時(shí)通訊的java實(shí)現(xiàn)方法

    本篇文章主要介紹了關(guān)于Socket的解析以及雙方通訊的java實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03

最新評(píng)論

富宁县| 仙居县| 南丰县| 十堰市| 逊克县| 盐津县| 金寨县| 武穴市| 绥阳县| 盐山县| 公安县| 那曲县| 松潘县| 寻甸| 徐州市| 阿拉善右旗| 涿州市| 游戏| 疏附县| 南郑县| 虎林市| 卫辉市| 吉安县| 合川市| 临高县| 太保市| 托克逊县| 苍南县| 大冶市| 兴城市| 武乡县| 新平| 渝北区| 加查县| 盈江县| 武冈市| 乌审旗| 宜良县| 麻城市| 宜兰县| 什邡市|