數據庫+SpringBean注入的方式實現策略模式--- 支付示例

在這裏插入圖片描述

  1. 首先定義個支付行爲接口 PayStragtegy
package com.mbh.first_boot_demo.strategy;

/**
 * 支付行爲接口
 * @description:
 * @author: mabh
 * @create: 2020/5/22 10:51 下午
 **/
public interface PayStrategy {

    /**
     * 調用支付
     */
    String toPayHtml();
}

  1. 繼承接口實現阿里支付行爲
package com.mbh.first_boot_demo.strategy.impl;

import com.mbh.first_boot_demo.strategy.PayStrategy;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: mabh
 * @create: 2020/5/22 10:55 下午
 **/
@Component
public class AliPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        System.out.println("調用阿里支付....");
        return "阿里巴巴";
    }
}

  1. 實現接口實現微信支付行爲
package com.mbh.first_boot_demo.strategy.impl;

import com.mbh.first_boot_demo.strategy.PayStrategy;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: mabh
 * @create: 2020/5/22 10:57 下午
 **/
@Component
public class WeixinPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        System.out.println("執行微信支付.....");
        return "微信支付";
    }
}

  1. 定義支付上文類,獲取具體支付策略行爲
package com.mbh.first_boot_demo.strategy;

import com.mbh.first_boot_demo.utils.SpringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付上下文
 * @description:
 * @author: mabh
 * @create: 2020/5/22 11:06 下午
 **/
@Component
public class PayContextStrategy {


    /**
     * 獲取具體策略實現
     * @param payCode
     * @return
     */
    public String toPayHtml(String payCode){
        if (StringUtils.isEmpty(payCode)) {
            return "payCode不能爲空";
        }
        //查詢數據庫具體策略實現,我用Map代替
        String paymentChannel = getPaymentChannel(payCode);
        if(paymentChannel == null){
            return "沒有查詢到支付渠道";
        }
        //正常來說這個Bean名稱是在數據庫查詢到的。這是用Map處理的。
        String strategyBeanId = paymentChannel;
        PayStrategy payStrategy = SpringUtils.getBean(strategyBeanId, PayStrategy.class);
        return payStrategy.toPayHtml();
    }


    /**
     *  查詢數據庫具體策略實現,我用Map代替
     * @param payCode
     * @return
     */
    private String getPaymentChannel(String payCode) {
        Map<String,String> map = new HashMap<>();
        map.put("wx_pay","weixinPayStrategy");
        map.put("ali_pay","aliPayStrategy");

        //key: 支付方式編碼
        //value: 支付類型策略Bean
        return map.get(payCode);
    }
}

package com.mbh.first_boot_demo.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * @description:
 * @author: mabh
 * @create: 2020/5/22 11:48 下午
 **/
public class SpringUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        this.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * 根據Class獲取Bean
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    /**
     * 根據Bean名稱獲取Bean
     *
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }

    /**
     * 根據Bean名稱獲取指定類型Class Bean
     * @param beanName
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String beanName,Class<T> clazz) {
        return getApplicationContext().getBean(beanName,clazz);
    }
}

使用方式直接在Controller 或 Serice 注入 PayContextStrategy類即可:

@Autowired
public PayContextStrategy payContextStrategy;

到這裏就結束了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章