日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot消除switch-case方法

發布時間:2024/9/15 javascript 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot消除switch-case方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Springboot消除switch-case方法

背景
最近,在使用springboot開發一個接口的時候,需要根據接收的請求事件類型,去執行不同的操作,返回不同的結果,基本邏輯如下:

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:}

寫完會發現,隨著事件的增加,這段代碼會很長,每個事件的處理函數也都集中在一個類當中,不好維護。因此,通過搜索學習發現,可以使用Springboot的注解+策略模式+簡單工廠的方式來消除switch-case。

重構

  • 定義結構體
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); }

所有的時間處理類都要實現這個接口。其中,execute是事件的處理方法

編寫具體的時間處理類

接下來,逐個的編寫事件處理類,舉下面一個例子:

@Component("appStartProcess") @CRSEventAnnotation(value = CRSEvent.APP_START) public class AppStartProcess implements EventProcess{@Overridepublic 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;}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.context = applicationContext;} }
  • 定義事件處理類工廠,用來生產各種事件處理對象
@Component public class EventProcessFactory {@AutowiredSpringContextUtil 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);} }
  • 調用代碼修改
CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent());EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent);if (eventProcess != null){return eventProcess.execute(crsRequest);} return null;

這樣,代碼就沒有了switch-case,增加一個事件也很簡單,只需要實現EventProcess接口即可。

原博客

總結

以上是生活随笔為你收集整理的Springboot消除switch-case方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。