使用策略模式+工厂模式替换一串串的if else 与 switch

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);
        // 输出结果:标准化信托
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

任错错

如果对您有帮助我很开心

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值