策略模式+工厂模式+Map+反射
发布日期:2021-11-11 12:48:06
浏览次数:1
分类:技术文章
本文共 1382 字,大约阅读时间需要 4 分钟。
1、策略接口
public interface IStartegy{ void run(String test);}
2、实现策略接口类
public class Startegy1 implements IStartegy{ @Override public void run(String test) { // 你的代码 }}
3、枚举类
public enum StartegyEnum { STRATEGY_1("STRATEGY_1","com.xxx.Startegy1"), STRATEGY_2("STRATEGY_2","com.xxx.Startegy2"), ; private String type; private String className; DataCheckFilterEnum(String type, String className) { this.type = type; this.className = className; } public String getType() { return type; } public String getClassName() { return className; }}
4、策略工厂
@Slf4jpublic class StrategyFactory { private static Mapmap = new HashMap<>(); static { for (StartegyEnum enum: StartegyEnum.values()) { try { Class clazz = Class.forName(enum.getClassName()); IStrategy iStrategy = (IStrategy) clazz.newInstance(); map.put(enum.getType(), iStrategy); } catch (ClassNotFoundException e) { log.error(e.getMessage(), e); } catch (IllegalAccessException e) { log.error(e.getMessage(), e); } catch (InstantiationException e) { log.error(e.getMessage(), e); } } } public static IStrategy getIStrategy(String type) { return map.get(type); }}
转载地址:https://blog.csdn.net/Carson073/article/details/121264851 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月02日 23时24分12秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
遇到硬盘数据丢失问题,选择EasyRecovery
2021-06-24
Shell echo命令
2021-06-24
萝卜保卫战3内购破解+Toast窗口增加(Love版)
2021-06-24
SVN 命令行的使用
2021-06-24
Laravel删除产品-CRUD之delete(destroy)
2019-04-21
appium的xpath定位问题及常用方法
2019-04-21
ibatis中#和$如何当作字符使用?
2019-04-21
数据库的导入导出
2019-04-21
采用最低松弛度优先调度的实时系统调度程序
2019-04-21
SCCM2012安装、配置
2019-04-21
更改SQL实例端口
2019-04-21
python开发_stat
2019-04-21
图像缩放的双线性内插值算法的原理解析
2019-04-21
QUrl不同版本之间的坑
2019-04-21
boost库(条件变量)
2019-04-21
future封装了callable,thread封装future。
2019-04-21
超酷的图像效果 (附demo; C#完成)
2019-04-21
1124 - Celebrity jeopardy
2019-04-21
知道什么时候该停下来!
2019-04-21