策略模式+工厂模式+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 Map
map = 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java开发PC端移动端同步微信授权登录注意的问题
下一篇:DataX快速入门Demo

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年03月02日 23时24分12秒