Springboot中如何消除switch-case-创新互联
这篇文章主要介绍“Springboot中如何消除switch-case”,在日常操作中,相信很多人在Springboot中如何消除switch-case问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Springboot中如何消除switch-case”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联公司于2013年开始,先为路南等服务建站,路南等地企业,进行企业商务咨询服务。为路南企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。基本逻辑如下:
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 MapeventProcessMap = 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”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
标题名称:Springboot中如何消除switch-case-创新互联
转载源于:http://azwzsj.com/article/hjepc.html