Springboot中如何消除switch-case
发表于:2025-11-14 作者:千家信息网编辑
千家信息网最后更新 2025年11月14日,这篇文章主要介绍"Springboot中如何消除switch-case",在日常操作中,相信很多人在Springboot中如何消除switch-case问题上存在疑惑,小编查阅了各式资料,整理出简单好
千家信息网最后更新 2025年11月14日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安全错误
数据库的锁怎样保障安全
核安全政策法规数据库
数据库默认配置
计算机网络技术未来职业了解
服务器安全模式删除杀毒软件
分布式数据库弹性计算
郑州校园网移动的服务器
达梦数据库 招聘
辛巴网络技术
阿里巴巴的交易网络安全措施
全国网络安全宣传周是哪一天
坦克世界闪击战服务器暂时不可用
众筹互联网科技有限公司
深圳易迅捷互联网科技
大学网络安全学院招聘
双鸭山精益管理软件开发
东南大学的网络安全专业好不好
模联网络安全战
沈阳升腾服务器厂商
数据库需求分析考勤表
爱数怎么还要服务器
文科计算机网络技术
2020级联考数据库
数据库的物理模型
广州科慧网络技术有限公司
社区网络安全共建项目
北京搜集客户外贸软件开发公司
服务器需要加隔离电源吗
水星路由器PPP服务器是什么
邮储软件开发中心有晚餐吗
服务器方面有什么证