自定义支付注解=消除if:策略模式+工厂模式+单例模式
代码取景,知识来源于视频《云析学院【路飞老师】-一次学习终身受用-如何正确使用设计模式写出优雅的代码》
1、定义注解标签 PayChannel
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PayChannel {
int channelId();
}
2、创建支付渠道策略接口及其不同实现类
1)定义价格接口PayStrategy
public interface PayStrategy {
/**
* 根据渠道计算价格
* @param channelId
* @param goodsId
* @return
*/
BigDecimal calRecharge(Integer channelId,Integer goodsId);
}
2)创建不同的价格接口实现类,
包名:com.yf.custom.pay.impl
工商银行
@PayChannel(channelId = 1)
public class ICBCBank implements PayStrategy{
@Override
public BigDecimal calRecharge(Intege