1.详解工厂模式与替换if else
具体业务:
case when TPROD.FST_LVL_PROD_CLS='04-信托' and TPROD.SEC_PROD_CLS='0402-信托计划' then '标准化产品'
when TPROD.FST_LVL_PROD_CLS='04-信托' and TPROD.SEC_PROD_CLS='0402-家庭信托' then '家族信托'
else '其他' end as SEC_PROD_CLS
1.1详解策略模式+工厂模式:
https://blog.csdn.net/qq_36250202/article/details/101022057?spm=1001.2014.3001.5501
通俗易懂,看完后要回来哦,你还没有学习怎么替换if esle
1.2 替换if esle
1.2.1 定义策略执行接口
public interface LsssxsxtStrategy {
// 定义策略执行方法
String algorithm(String param);
}
1.2.2实现具体策略
public class Lsssxsxtjz implements LsssxsxtStrategy {
@Override
public String algorithm(String param) {
System.out.println("产品类型:"+"家族信托");
return "家族信托";
}
}
public class Lsssxsxtbz implements LsssxsxtStrategy {
@Override
public String algorithm(String param) {
System.out.println("产品类型:"+"标准化产品");
return "标准化产品";
}
}
1.2.3定义封装工厂类
public class LsssxsxtFactory {
// 定义策略map缓存
private static final Map<String, LsssxsxtStrategy> lsssxlsxtStrategies = new HashMap<>();
static {
lsssxlsxtStrategies .put("04-信托,0402-信托计划", new Lsssxsxtbz());
lsssxlsxtStrategies .put("04-信托,0401-家庭信托", new LsssxsxtjZ());
}
// 获取指定策略
public static LsssxsxtStrategy getShareStrategy(String type) {
if (type == null || type.isEmpty()) {
throw new IllegalArgumentException("type should not be empty.");
}
return lsssxlsxtStrategies.get(type);
}
public static void main(String[] args) {
// 测试demo
String shareType = "04-信托,0402-信托计划";
LsssxsxtStrategy shareStrategy = LsssxsxtFactory.getShareStrategy(shareType);
String algorithm = shareStrategy.algorithm(shareType);
if(algorithm==null||algorithm.isEmpty()){
algorithm ="其他";
}
System.out.println(algorithm);
// 输出结果:标准化信托
}
}