温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Springboot消除switch-case的过程解析

发布时间:2021-09-14 22:59:16 来源:亿速云 阅读:104 作者:chen 栏目:编程语言

本篇内容介绍了“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工具类

@Componentpublic class SpringContextUtil implements ApplicationContextAware{  private ApplicationContext context;  public ApplicationContext getContext(){    return context;  }  @Override  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {    this.context = applicationContext;  }}

定义事件处理类工厂,用来生产各种事件处理对象

@Componentpublic 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接口即可。

“Springboot消除switch-case的过程解析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI