策略模式
枚举类
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();
}
}
}
博主公众号
求关注