springboot 中的 这种策略模式运用实战
说明问题之前,我们先来看一个小的demo:
首先创建一个简单springboot web 项目, 主要代码如下:
新建一个接口 实现
process
功能
public interface Strategy {void process(Object o);
}
添加 process
功能 的三种不同实现方式
@Component("PLAN_A")
public class StrategyImplA implements Strategy {@Overridepublic void process(Object o) {System.out.println("StrategyImplA");}
}
@Component("PLAN_B")
public class StrategyImplB implements Strategy {@Overridepublic void process(Object o) {System.out.println("StrategyImplB");}
}
@Component("PLAN_C")
public class StrategyImplC implements Strategy {@Overridepublic void process(Object o) {System.out.println("StrategyImplB");}
}
StrategyContext 中可以通过实现 afterPropertiesSet() 方法,看到属性已被注入。其中 strategyMap中key 为属性的名称,value为 具体实现。所以,我们就可以通过这个方式 来实现策略方案了。是不是很惊艳!!!!!
再来一个 补充的地方,就是如果 我们获取具体方法策略的时候,没有获取到具体策略 怎么办呢?这个时候 我们就想到了线程池的拒绝策略实现,如果找不到就给一种默认的策略实现。
调整后的实现如下:
@Component
public class StrategyContext implements InitializingBean, ApplicationContextAware {private static Strategy DEFAULT_STRATEGY = new DefaultStrategy();private ApplicationContext applicationContext;@Resourceprivate Map<String, Strategy> STRATEGY_MAP = new HashMap<>(4);@Overridepublic void afterPropertiesSet() throws Exception {STRATEGY_MAP = applicationContext.getBeansOfType(Strategy.class);}@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public Strategy getStrategy(String strategyName) {return STRATEGY_MAP.get(strategyName) == null ? DEFAULT_STRATEGY : STRATEGY_MAP.get(strategyName);}static class DefaultStrategy implements Strategy {@Overridepublic void process(Object o) {System.out.println("default PLAN");}}
}
这种就是为了防止 调用方 没有获取到对应的处理策略 出现空指针异常,或者其他不可控的行为。
测试
@SpringBootTest
class StrategyBootApplicationTests {@AutowiredStrategyContext strategyContext;@Testvoid contextLoads() {Strategy strategy = strategyContext.getStrategy("PLAN_A");strategy.process(1L);strategy = strategyContext.getStrategy("PLAN_E");strategy.process(2L);}
}
输出
StrategyImplA
default PLAN
https://github.com/andanyoung/springboot/tree/master/strategy-boot
springboot 中的 这种策略模式运用实战相关推荐
- SpringBoot中实现简单策略模式
策略模式(Strategy Pattern):一个类的行为或其算法可以在运行时更改,策略模式属于行为型模式. 策略模式简单Java实现: https://www.runoob.com/design-p ...
- SpringBoot中配置为开发模式,代码修改后不用重新运行
场景 SpringBoot中配置为开发模式,修改代码后不用重新运行. 实现 打开pom.xml,添加依赖 <!-- 添加如下依赖,配置为开发模式,代码做了修改,不用重新运行--><! ...
- 从java多态到策略模式_设计模式中的多态——策略模式详解
2. 策略模式详解 2.1 策略模式定义 策略模式定义了一系列算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户端而独立的变化. 可以使用多态进行类比来理解策略模 ...
- 工作中的设计模式 —— 策略模式
前言 返利网站 https://m.cpa5.cn/ 策略模式是一种行为设计模式,它能让你定义一系列算法,并将每种算法分别放入独立的类中,以使算法的对象能够相互替换. 使用场景 策略模式在工作中使用的 ...
- springboot集成clickhouse之策略模式+线程池+反射实现动态批量操作
线程池+策略模式+反射实现动态批量操作clickhouse 目录 前言 安装ck 整体设计 策略模式设计 线程池管理ck 码上有戏 反射核心 测试 简单说明 源码地址 目录 前言 之前实际项目中遇到过 ...
- 设计模式在Netty 中的应用-策略模式源码举例
策略模式要点回顾: 1.封装一系列可相互替换的算法家族. 2.动态选择某一个策略. 案例分析: public final class DefaultEventExecutorChooserFactor ...
- springboot中hibernate validator校验模式,分组校验,自定义校验
检验模式 上面例子中一次性返回了所有验证不通过的集合,通常按顺序验证到第一个字段不符合验证要求时,就可以直接拒绝请求了.Hibernate Validator有以下两种验证模式: 普通模式(默认是这个 ...
- 设计模式(三)策略模式——在Spring中使用策略模式
前言 在 设计模式(一)策略模式 -- 策略模式结构 和 设计模式(二)策略模式 -- 在程序中通过枚举使用策略模式 两篇博文中分析了策略模式的基础使用,在实际的项目开发中要结合spring容器使用策 ...
- 如何理解和运用策略模式
作者:溪~源 blog.csdn.net/xuan_lu/article/details/114417396 定义 首先介绍一下众多博客文章中提到的策略模式的概念,针对于文章结构构成,请允许小编在此 ...
最新文章
- 【水】JSOI完美的对称
- Qt中用ODBC连接excel中文乱码问题
- libpcap 源代码分析(二)
- SQL中的Exist操作
- MATLAB中将pgm图片二值化
- HarmonyOS之常用组件ProgressBar的功能和使用
- 多线程——线程的生命周期
- Linux虚拟机安装应用程序提示Graphical installers are not supported by the vm
- pageResponse - 让H5适配移动设备全家(移动端适配)
- 最新csdnJS逆向分析
- mac ~/.bash_profile 重启失效
- matlab简单分析短时傅里叶变换STFT
- 值得推荐的Idea十几大优秀插件
- 微信小游戏开发新手教程1-人人都能做游戏
- mongodb 两小时入门
- 推荐使用Windows10企业版LTSC的理由
- 【工作】Amazon Fraud Detection
- Connect internal only, until freed错误处理
- sonar mysql svn_Sonar本地环境搭建
- CCF NOI1041. 志愿者选拔【sort】
热门文章
- MPB:南农韦中组-根际细菌便利和竞争互作类型和强度的研究方法
- 好友国自然基金获批!畅饮之余透露给我这个秘密
- 终极大招——怎么在学术会议上有所收获?
- 中国林科院亚热带林业研究所林木根际微生物博士后招聘启事
- python使用matplotlib可视化堆积的折线图、使用stackplot函数可视化堆积的折线图、不同数据在垂直方向堆叠
- R语言构建logistic回归模型:WVPlots包PRTPlot函数可视化获取logistic回归模型的最优阈值、优化(precision、enrichment)和recall之间的折衷
- R语言ggplot2可视化把y轴显示在图像的右侧实战: y-axis on the right
- AttributeError: h5py.h5.H5PYConfig‘ has no attribute ‘__reduce_cython__‘
- R语言基于随机森林进行特征选择(feature selection)
- RStudio配置R程序包的默认下载源