Springboot中如何消除switch-case
发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍"Springboot中如何消除switch-case",在日常操作中,相信很多人在Springboot中如何消除switch-case问题上存在疑惑,小编查阅了各式资料,整理出简单好
千家信息网最后更新 2026年11月04日Springboot中如何消除switch-case
这篇文章主要介绍"Springboot中如何消除switch-case",在日常操作中,相信很多人在Springboot中如何消除switch-case问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Springboot中如何消除switch-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工具类
@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 eventProcessMap = new ConcurrentHashMap<>(); public EventProcessFactory() { Map 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"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
事件
处理
学习
代码
接口
接下来
工厂
方法
时间
更多
注解
帮助
实用
不好
例子
函数
对象
工具
文章
方式
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
小米移动互联网科技园
河南云网互联网络科技有限公司
港股互联网科技股
互联网科技金融
有马互联网科技
远信互联网科技
凡科互联网科技
火传媒上海互联网科技
美国互联网前沿科技
深圳卡尔迅互联网科技有限公司怎么样
老年人脱离互联网科技的作文
银川新华互联网科技学校地址
全球顶尖互联网科技公司
国大互联网教育科技有限公司工作
长沙互联网科技
中科明德北京互联网科技
互联网企业科技类企业如何做账
互联网领先科技宣传片
玉溪新华互联网科技怎么选
盐城市攀科互联网科技有限公司
互联网科技网
科技与互联网
互联网 视角下的科技创新
猫创互联网科技
互联网科技论坛
互联网科技股午盘股票涨跌
互联网科技人工智能实验室
内蒙古九一互联网科技
互联网方面的科技前沿
重庆科技产业互联网有哪些