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{

    @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;
    }
}

定義事件處理類工廠,用來生產各種事件處理對象

@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);
    }
}

調用代碼修改

 CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent());
 EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent);
 if (eventProcess != null){
     return eventProcess.execute(crsRequest);
 }
return null;

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM