美文网首页
基于策略模式的可插拔设计2

基于策略模式的可插拔设计2

作者: EmilioWong | 来源:发表于2018-04-18 11:21 被阅读0次

针对上篇最后提出的不足,改进如下

  1. ModuleManager增加spareStrategyName表示备用策略,BaseStrategy去掉spare的相关方法。改动较为简单,代码略。
  2. 增加SingleAPIModuleStrategy类为简单的单一api接口做进一步的封装
    SingleAPIModuleStrategy代码如下
public abstract class SingleAPIModuleStrategy extends BaseStrategy {

    protected SupplyAPI api;

    protected JSONObject apiResponse;

    public SingleAPIModuleStrategy(Report report, SupplyAPIRepository supplyAPIRepository, APISearchRepository apiSearchRepository) {
        super(report, supplyAPIRepository, apiSearchRepository);
        setAPI();
    }

    protected abstract void setAPI();

    @Override
    public boolean isAPIActive() {
        return api.isActive() && isParameterComplete();
    }

    protected abstract boolean isParameterComplete();

    @Override
    public boolean isContainsAPI(Map<String, JSONObject> pool) {
        return pool.containsKey(api.getCode());
    }

    @Override
    public boolean isAPIUnfinished(Map<String, JSONObject> pool) {
        return EMPTY_JSON.equals(pool.get(api.getCode()));
    }

    @Override
    public void tryPutEmptyAPI(Map<String, JSONObject> pool) {
        if (!pool.containsKey(api.getCode())) {
            pool.put(api.getCode(), EMPTY_JSON);
        }
    }

    @Override
    public void removeEmptyAPI(Map<String, JSONObject> pool) {
        pool.remove(api.getCode());
    }

    @Override
    public void putAPIResponseIntoPool(Map<String, JSONObject> pool) {
        if (apiResponse != null && !EMPTY_JSON.equals(apiResponse)) {
            pool.put(api.getCode(), apiResponse);
        }
    }
}

对应的FraudModuleStrategy例子如下

public class FraudModuleStrategy extends SingleAPIModuleStrategy {

    private static final Logger logger = LoggerFactory.getLogger(FraudModuleStrategy.class);

    private final static long API_ID = 17L;

    public FraudModuleStrategy(Report report, SupplyAPIRepository supplyAPIRepository, APISearchRepository apiSearchRepository) {
        super(report, supplyAPIRepository, apiSearchRepository);
    }

    @Override
    protected void setAPI() {
        super.api = supplyAPIRepository.findOne(API_ID);
    }

    @Override
    protected boolean isParameterComplete() {
        return super.report.getCustomerName() != null && super.report.getCustomerIdCard() != null
                && super.report.getCustomerMobile() != null;
    }

    @Override
    public boolean fetchData(Map<String, JSONObject> pool) {
        JSONObject cacheData = pool.get(api.getCode());
        if (cacheData != null && !EMPTY_JSON.equals(cacheData)) {
            super.apiResponse = cacheData;
            return true;
        }
        API api = new API();
        api.setSupplyAPI(super.api);
        api.getParameters().put("name", super.report.getCustomerName());
        api.getParameters().put("idCard", super.report.getCustomerIdCard());
        api.getParameters().put("mobile", super.report.getCustomerMobile());
        if (!StringUtils.isEmpty(report.getCustomerBankCard())) {
            api.getParameters().put("bankCardNo", super.report.getCustomerBankCard());
        }

        cacheData = getCache(api);
        if (cacheData != null) {
            logger.info("缓存查询" + super.api.getCode());
        } else {
            logger.info("即时查询" + super.api.getCode());
            XinShuCallable xinShuCallable = new XinShuCallable(api.getSupplyAPI(), new HashMap<>(), api.getParameters(),
                    "");
            cacheData = xinShuCallable.call();
            logger.info(super.api.getCode() + " response:" + cacheData);
            if (cacheData != null && "0000".equals(cacheData.getString("rc"))) {
                putCache(api, cacheData);
            }
        }
        if (cacheData == null) {
            return false;
        } else {
            super.apiResponse = cacheData;
            return true;
        }
    }

    @Override
    public BaseModule analyseData() {
        //分析及封装操作,省略
    }

    @Override
    public void setModuleIntoReport(BaseModule module, Integer displayOrder) {
        module.setDisplaySort(displayOrder);
        report.setFraudModule((FraudModule) module);
    }
}

改动后,最主要的变化是主策略和备用策略通过修改ModuleManager类的反射字段也实现了可插拔。同时,如果有同一模块多中策略的话,其实可以参考链表的形式,实现策略的无限增加。但鉴于实际情况,本次就只到两个策略就结束了。

相关文章

  • 基于策略模式的可插拔设计2

    针对上篇最后提出的不足,改进如下 ModuleManager增加spareStrategyName表示备用策略,B...

  • 基于策略模式的可插拔设计

    业务背景 公司需要将n个api接口返回的数据在分析后封装成个信报告。原先的设计比较死板,几个版本需要查的东西是固定...

  • 构建自己的交易策略模型

    基于策略设计模式的波动市场交易模型: 按照策略设计模式至少需要需要以下几个类: 环境类 包裹数据与策略池; 数据类...

  • 11.7设计模式-策略模式-详解

    设计模式-策略模式 策略模式详解 策略模式在android中的实际运用 1.策略模式详解 2.策略模式在andro...

  • 网站设计解构:有效的交互设计框架和模式

    感觉就是说的设计的规范吧 1.可重用策略:模式,组件,交互设计的框架体系 2.设计模式:模式名称,描述,上下文情境...

  • 设计模式

    软件开发中常用设计模式和设计原则有哪些? ##设计模式: * 1、简单工厂模式(Factory) * 2、策略模式...

  • Modularity

    发自简书Hyperledger Fabric经过专门设计,具有模块化架构。 无论是可插拔的共识,可插拔的身份管理协...

  • 策略模式Strategy Pattern

    从头回顾一下设计模式。 策略模式Strategy Pattern 策略模式定义了算法族,分别封装起来,让它们之间可...

  • go-micro简介

    go-micro简介Go Micro是一个插件化的基础框架,基于此可以构建微服务,Micro的设计哲学是可插拔的插...

  • 简说设计模式之策略模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是策略模式 策略(Strategy)模式...

网友评论

      本文标题:基于策略模式的可插拔设计2

      本文链接:https://www.haomeiwen.com/subject/rekvkftx.html