Java 策略模式

2021-02-09  本文已影响0人  索性流年

文集地址

一句话总结策略模式

什么是策略模式?

实现过程

优点

缺点

总结

案例聚合支付

前提

/*
* 共同算法定义骨架
*
* */
public interface PayStrategy {
    /*共同算法骨架*/
    public String toPayhtml();
}

/*阿里支付算法*/
@Component
public class AliPayStrategy implements PayStrategy {
    @Override
    public String toPayhtml() {
        return "调用支付宝支付方法";
    }
}

/*银联支付算法*/
@Component
public class YinLianPayStrategy implements PayStrategy {
    @Override
    public String toPayhtml() {
        return "调用银联支付方式";
    }
}

@Component
@Mapper
public interface PaymentChannelMapper {

    @Select("SELECT * FROM payment_channel AS pay WHERE pay.payId = #{payCode}")
    PaymentChannelEntity getPaymentChannel(String payCode);
}

//实体类
public class PaymentChannelEntity {
    private String id;
    private String payId;
    private String payType;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPayId() {
        return payId;
    }

    public void setPayId(String payId) {
        this.payId = payId;
    }

    public String getPayType() {
        return payType;
    }

    public void setPayType(String payType) {
        this.payType = payType;
    }
}

/*使用Bend对象获取Spring容器中对应的类*/
@Component
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    /*获取context上下文*/
    public static ApplicationContext getApplicationContext(){
        return context;
    }

    /*通过name获取Bean*/
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    /*通过类名获取Bean*/
    public static <T>T getBean(Class<T> className){
        return getApplicationContext().getBean(className);
    }

    /*通过name,以及Class返回指定Bean*/
    public static <T>T getBean(String neme, Class<T> className){
        return getApplicationContext().getBean(neme,className);
    }
}

新建.class作用于获取对象实现类

@Component
public class PayContextStrategy {
    @Autowired
    public PaymentChannelMapper paymentChannelMapper;

    @Autowired
    public SpringUtils springUtils;

    public PayContextStrategy() {
        super();
    }

    public String toPayHtml(String payCode){
        System.err.println(payCode);
//        使用PayCode查询数据库获取Bendid
        PaymentChannelEntity paymentChannel = paymentChannelMapper.getPaymentChannel(payCode);
//        获取bindid,使用spring容器获取实例方法
        if (paymentChannel == null){
            return "没有该渠道信息";
        }
        String strategyBeanId = paymentChannel.getPayType();
        System.err.println(strategyBeanId);
        if (StringUtils.isEmpty(strategyBeanId)){
            return "该渠道配置ben配置";
        }
        PayStrategy payStrategy =  springUtils.getBean(strategyBeanId,PayStrategy.class);
        return payStrategy.toPayhtml();
    };
}

新建.class 控制器作用于接口测试

@RestController
public class PayController {
    @Autowired
    private PayContextStrategy contextStrategy;

    @GetMapping("/toPayHtml")
    public String toPayHtml(String payCode){
        if (StringUtils.isEmpty(payCode)){
            return "渠道code不能为空";
        }
        return contextStrategy.toPayHtml(payCode);
    }
}

总结

案例思路

上一篇下一篇

猜你喜欢

热点阅读