Springboot eliminate switch-case method

Springboot eliminate switch-case method

BACKGROUND
Recently, the development in the use of an interface springboot, upon request, the type of event received, to perform different operations, returns a different result, the basic logic is as follows:

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

Finished find that with the increase of the event, this code will be very long, each event handlers are concentrated in a class among poor maintenance. Therefore, the study found through search, you can use the strategy pattern Springboot comment + + simple factory way to eliminate switch-case.

Reconstruction

  • Definition of the structure
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;
    }
}
  • Defines a comment
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface CRSEventAnnotation {

    CRSEvent value();
}
  • Defined event processing interface
public interface EventProcess {
    CRSResponse execute(CRSRequest resquest);
}

All times handler class must implement this interface. Where, execute a processing method events

Preparation of a specific time to deal with class

Then, one by one to write event handler class, a move the following example:

@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;
    }
}
  • Defined SpringContext Tools
@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;
    }
}
  • Defined event handler class factories for the production of processed events
@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);
    }
}
  • Call the code changes
 CRSEvent crsEvent = CRSEvent.valueOf(crsRequest.getEvent());
 EventProcess eventProcess = EventProcessFactory.createEventProcess(crsEvent);
 if (eventProcess != null){
     return eventProcess.execute(crsRequest);
 }
return null;

In this way, the code will be no switch-case, add an event is also very simple, only need to implement EventProcess interfaces can be.

Original blog

Guess you like

Origin www.cnblogs.com/nxzblogs/p/11681974.html
Recommended