首页 Java java教程 Springboot中如何消除switch-case

Springboot中如何消除switch-case

May 14, 2023 pm 07:49 PM
switch springboot case

基本逻辑如下:

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

以上是Springboot中如何消除switch-case的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

switch日版港版有什么区别 switch日版港版有什么区别 Jun 20, 2023 pm 02:06 PM

switch日版港版的区别:1、充电器上存在主要区别,日版和国标充电口通用,港版使用英式三角插头;2、日版使用点卡支付,而港服使用支付宝支付;3、港版售后保修需要邮寄回HK售后点,而日版需要邮寄到Japan指定售后点。

switch连电视没反应怎么办 switch连电视没反应怎么办 Jul 03, 2023 am 11:15 AM

switch连电视没反应解决方法:1、检查switch和电视的电源是否连接;2、检查电视HDMI线接口是否插紧;3、Switch底座后盖打开,检查电源线和HDMI线是否插紧;4、检查Switch是否开机状态下放入底座;5、检查电视是否切换了信号源。

艾尔登法环switch能玩么 艾尔登法环switch能玩么 Mar 11, 2024 am 11:31 AM

艾尔登法环能在switch上游玩吗?艾尔登法环作为一款极具魅力的动作RPG游戏,不少朋友可能还不明白它能否在switch平台上进行畅快游玩,答案是暂时无法实现。艾尔登法环switch能玩么答:不能在switch游玩。此款备受瞩目的魂系列角色扮演类动作游戏已正式发布,玩家可前往PC、ps4/5以及XboxSerieseX|S/XboxOne购买并立即体验。许多拥有switch的朋友们可能仍热切期待在ns上畅享这款游戏,但遗憾的说,该游戏并无switch版本。据官网配置要求显示,游戏配置较高,而sw

switch可以一直放在底座上充电吗 switch可以一直放在底座上充电吗 Jul 06, 2023 pm 04:51 PM

switch不可以一直放在底座上充电,其危害有:1、缩短电池的寿命和续航时间;2、导致内存烧坏。

SpringBoot与SpringMVC的比较及差别分析 SpringBoot与SpringMVC的比较及差别分析 Dec 29, 2023 am 11:02 AM

SpringBoot和SpringMVC都是Java开发中常用的框架,但它们之间有一些明显的差异。本文将探究这两个框架的特点和用途,并对它们的差异进行比较。首先,我们来了解一下SpringBoot。SpringBoot是由Pivotal团队开发的,它旨在简化基于Spring框架的应用程序的创建和部署。它提供了一种快速、轻量级的方式来构建独立的、可执行

switch32g内存够用吗 switch32g内存够用吗 Jun 20, 2023 pm 02:28 PM

switch32g内存不够用,其原因如下:1、买数字版游戏想购买DLC截图储存到掌机里的话根本不够用;2、下载数字版游戏,32G内存大约可以储存2~3个游戏,根本满足不了玩游戏的需求;3、数字游戏及其dlc一般保持在5G,除过自带系统占用的空间,下载游戏对于玩家来说比较困难。

SpringBoot+Dubbo+Nacos 开发实战教程 SpringBoot+Dubbo+Nacos 开发实战教程 Aug 15, 2023 pm 04:49 PM

本文来写个详细的例子来说下dubbo+nacos+Spring Boot开发实战。本文不会讲述太多的理论的知识,会写一个最简单的例子来说明dubbo如何与nacos整合,快速搭建开发环境。

switch lite和switch有什么区别 switch lite和switch有什么区别 Jun 28, 2023 pm 02:13 PM

switch lite和switch区别有:1、尺寸不同;2、屏幕大小及机身重量不同;3、手柄是否可拆卸及手柄功能不同;4、电池续航能力不同;5、手柄按键设计不同;6、可支持游戏不同;6、颜色不同。

See all articles