基于springboot策略模式+工厂模式

参考链接

策略模式
枚举类
package per.jcloud.enums;

public enum DbSourceEnums {

    /*
     * 背不出来。。。
     */
    MYSQL(1,"mysql.driver"),

    HIVE(2,"hive2.driver");


    /**
     * 状态值
     */
    private int code;

    /**
     * 类型描述
     */
    private String value;

    private DbSourceEnums(int code, String value) {
        this.code = code;
        this.value = value;
    }
    public int getCode() {
        return code;
    }

    public String getValue() {
        return value;
    }

    public static DbSourceEnums valueOf(int code) {
        for (DbSourceEnums type : DbSourceEnums.values()) {
            if (type.getCode()==code) {
                return type;
            }
        }
        return null;
    }
}

策略接口
package per.jcloud.service;

public interface DbDataxJsonService {
    public String getJsonString();
}

实现类
package per.jcloud.service.Impl;

import per.jcloud.service.DbDataxJsonService;

public class MysqlDataxJsonServiceImpl implements DbDataxJsonService {
    @Override
    public String getJsonString() {
        return "mysql";
    }
}

package per.jcloud.service.Impl;

import per.jcloud.service.DbDataxJsonService;

public class HiveDataxJsonServiceImpl implements DbDataxJsonService {
    @Override
    public String getJsonString() {
        return "hive";
    }
}

工厂类(反射的方式)

其余方式可以看参考链接

package per.jcloud.factory;

import per.jcloud.enums.DbSourceEnums;
import per.jcloud.service.DbDataxJsonService;
import per.jcloud.service.Impl.HiveDataxJsonServiceImpl;
import per.jcloud.service.Impl.MysqlDataxJsonServiceImpl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DbSourceServiceFactory {

    private static Map<String, Class<? extends DbDataxJsonService>> services = new ConcurrentHashMap<>();

    //初始化map,存放策略
    static {
        services.put(DbSourceEnums.MYSQL.getValue(), MysqlDataxJsonServiceImpl.class);
        services.put(DbSourceEnums.HIVE.getValue(), HiveDataxJsonServiceImpl.class);
    }

    //获取策略
    public static DbDataxJsonService getByType(String type) {
        try {
            Class<? extends DbDataxJsonService> userPayServiceClass = services.get(type);
            return userPayServiceClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return new MysqlDataxJsonServiceImpl();
        }

    }

}
测试
package per.jcloud.factory;

import per.jcloud.enums.DbSourceEnums;
import per.jcloud.service.DbDataxJsonService;

public class TestFactory {
    public static void main(String[] args){

        DbDataxJsonService dbDataxJsonService = DbSourceServiceFactory.getByType(DbSourceEnums.MYSQL.getValue());

        System.out.println(dbDataxJsonService.getJsonString());

    }
}

变化

1.修改为抽象方法
2.上述方式未交给spring容器管理,存在实现类中的mapper注入为空问题

修改策略接口
@Service
public abstract class AbsDbDataxJsonService {

    @Resource
    private XXXMapper mapper;

    @Autowired
    private DataNodeMapper dataNodeMapper;
    public  static AbsDbDataxJsonService absDbDataxJsonService;

    @PostConstruct
    public void init(){
        absDbDataxJsonService = this;
        absDbDataxJsonService.dataNodeMapper = this.dataNodeMapper;
    }

	public void(){
	//普通业务方法
	}
	

    public abstract String getJsonString();
}
实现类中用法

public class MysqlDataxJsonServiceImpl extends AbsDbDataxJsonService {
    @Override
    public String getJsonString() {
      实体类 =  absDbDataxJsonService.xxxmapper.xxx方法;
        return "mysql";
    }
}
工厂类
package per.jcloud.factory;

import per.jcloud.enums.DbSourceEnums;

import per.jcloud.service.AbsDbDataxJsonService;
import per.jcloud.service.Impl.HiveDataxJsonServiceImpl;
import per.jcloud.service.Impl.MysqlDataxJsonServiceImpl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DbSourceServiceFactory {

    private static Map<String, Class<? extends AbsDbDataxJsonService>> services = new ConcurrentHashMap<>();

    //初始化map,存放策略
    static {
        services.put(DbSourceEnums.MYSQL.getValue(), MysqlDataxJsonServiceImpl.class);
        services.put(DbSourceEnums.HIVE.getValue(), HiveDataxJsonServiceImpl.class);
    }

    //获取策略
    public static AbsDbDataxJsonService getByType(String type) {
        try {
            Class<? extends AbsDbDataxJsonService> aClass = services.get(type);
            return aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return new MysqlDataxJsonServiceImpl();
        }

    }

}

博主公众号
求关注
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值