天天看點

政策設計模式_政策設計模式

政策設計模式:解決多重if判斷

政策設計模式_政策設計模式
//資料庫
CREATE TABLE `payment_channel` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `channel_name` varchar(32) NOT NULL COMMENT '管道名稱',
  `channel_id` varchar(32) NOT NULL COMMENT '管道ID',
  `strategy_bean_id` varchar(255) DEFAULT NULL COMMENT '政策執行beanid',
  PRIMARY KEY (`id`,`channel_id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='支付管道 ';

-- ----------------------------
-- Records of payment_channel
-- ----------------------------
INSERT INTO `payment_channel` VALUES ('1', '支付寶管道', 'ali_pay', 'aliPayStrategy');
INSERT INTO `payment_channel` VALUES ('2', '微信支付管道', 'weChat_pay', 'weChatPayStrategy');
//實體類
@Data
public class PaymentChannelEntity {
    /** ID */
    private Integer id;
    /** 管道名稱 */
    private String channelName;
    /** 管道ID */
    private String channelId;
    /**
     * 政策執行beanId
     */
    private String strategyBeanId;
}
//dao層
public interface PaymentChannelMapper {


    @Select("SELECT id, channel_name as channelName, channel_id as channelId, strategy_bean_id as strategyBeanId  FROM payment_channel where channel_id=#{payType}")
     PaymentChannelEntity getPaymentChannel(String payType);
}
public interface PayStrategy {
    /**
     * 共同的方法
     */
    String toPayHtml();
}
@Component
public class AliPayStrategy implements PayStrategy {

    @Override
    public String toPayHtml() {
        String result="支付寶支付。。。。。。。。。。";
        return result;
    }
}
@ Component
public class WeChatPayStrategy implements PayStrategy {
    @Override
    public String toPayHtml() {
        String result="微信支付。。。。。。。。。。";
        return result;
    }
}
@ Component
public class PayContextStrategy {
    @Autowired
    private PaymentChannelMapper paymentChannelMapper;

    /**
     * 擷取具體政策實作
     *
     * @return
     */
    public String toPayHtml(String payCode) {
        if (StringUtils.isEmpty(payCode)) {
            return "payCode不能為空!.....";
        }
        // 1.查詢資料庫擷取具體政策實作
        PaymentChannelEntity paymentChannel = paymentChannelMapper.getPaymentChannel(payCode);
        if (paymentChannel == null) {
            return "沒有查詢支付管道";
        }
        // 擷取spring注入的bean的id
        String strategyBeanId = paymentChannel.getStrategyBeanId();
        System.err.println("strategyBeanId========"+strategyBeanId);
        if (StringUtils.isEmpty(strategyBeanId)) {
            return "資料庫沒有配置strategyBeanId";
        }
        PayStrategy payStrategy = SpringUtils.getBean(strategyBeanId, PayStrategy.class);
        return payStrategy.toPayHtml();
    }

}
@ RestController
public class PayController {


    @Autowired
    private PayContextStrategy payContextStrategy;

@GetMapping("/toPayHtml")
    public String toPayHtml(String payType){

        String toPayHtml = payContextStrategy.toPayHtml(payType);
        return toPayHtml;
    }
}
//工具類
@ Component
public class SpringUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    //擷取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通過name擷取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
    }

    //通過class擷取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }

    //通過name,以及Clazz傳回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
//application.yml
###服務啟動端口号
server:
  port: 8080
spring:
###資料庫相關連接配接      
  datasource:
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/factory?useUnicode=true&characterEncoding=utf-8&useSSL=false
           

繼續閱讀