设计模式【处理器责任链篇】

1.基于任务规则处理不同任务

①定义接口

public interface RuleHandler {

    /**
     * 任务名称
     *
     * @return
     */
    String getName();

    /**
     * 查询是否有该任务
     *
     * @param hasFeatureList  用户已有特征
     * @param benefitTaskDTO  任务
     * @param userId          用户id
     * @return BenefitTaskDTO 拥有任务,null 未拥有任务
     */
    boolean checkRuleResult(List<String> hasFeatureList, BenefitTaskDTO benefitTaskDTO, String userId);

    /**
     * 校验任务编码
     *
     * @param benefitTaskDTO 任务
     * @return
     */
    boolean checkRuleBefore(BenefitTaskDTO benefitTaskDTO);
}

②创建抽象

@Slf4j
public abstract class AbstractRuleHandler implements RuleHandler {

    protected String name;

    @PostConstruct
    protected void init() {
        this.name = this.getClass().getName();
    }

    @Override
    public String getName() {
        return this.name;
    }

    /**
     * 规则校验
     *
     * @param hasFeatureList 用户已有特征
     * @param benefitTaskDTO 任务
     * @param userId         用户id
     * @return
     */
    public boolean checkRule(List<String> hasFeatureList, BenefitTaskDTO benefitTaskDTO, String userId) {
        //跳过当前规则校验
        boolean ruleResult = true;
        try {
            if (checkRuleBefore(benefitTaskDTO)) {
                ruleResult = checkRuleResult(hasFeatureList, benefitTaskDTO, userId);
            }
            //如果校验未通过或者是最后一个规则器,返回规则处理结果
            if (!ruleResult || getNextRuleHandle() == null) {
                return ruleResult;
            }
            return getNextRuleHandle().checkRule(hasFeatureList, benefitTaskDTO, userId);
        } catch (Exception e) {
            log.error("checkRule is failed, userId:{}, benefitTask:{}, hasFeatureList:{}", userId, JSONObject.toJSONString(benefitTaskDTO), hasFeatureList, e);
        }
        return false;
    }

    /**
     * 规则校验
     *
     * @param hasFeatureList 用户已有特征
     * @param benefitTaskDTO 任务
     * @param userId         用户id
     * @return BenefitTaskDTO 规则校验通过拥有任务,null 规则校验不通过未拥有任务
     */
    @Override
    public abstract boolean checkRuleResult(List<String> hasFeatureList, BenefitTaskDTO benefitTaskDTO, String userId);

    /**
     * 检验任务
     *
     * @param benefitTaskDTO 任务
     * @return
     */
    @Override
    public abstract boolean checkRuleBefore(BenefitTaskDTO benefitTaskDTO);

    /**
     * 下一个规则执行器
     *
     * @return
     */
    protected abstract AbstractRuleHandler getNextRuleHandle();

}

③创建通用处理器

@Slf4j
@Component
public class RuleHandlerManager {

    @Resource(name = "ruleHandler4Feature")
    private AbstractRuleHandler ruleHandler;

    /**
     * 规则处理
     *
     * @param hasFeatureList  用户拥有特征
     * @param benefitTaskDTO  任务
     * @param userId          用户id
     */
    public boolean checkRule(List<String> hasFeatureList, BenefitTaskDTO benefitTaskDTO, String userId) {
        //校验是否执行规则校验
        return ruleHandler.checkRule(hasFeatureList, benefitTaskDTO, userId);
    }

}

④对应规则

@Slf4j
@Component
public class RuleHandler4Feature extends AbstractRuleHandler {

    @Resource(name = "ruleHandler4VisibleUser")
    private AbstractRuleHandler ruleHandler;

    @Override
    public boolean checkRuleResult(List<String> hasFeatureList, BenefitTaskDTO benefitTaskDTO, String userId) {
        //用户存在对应的人群特征
        boolean hasFeature = !CollectionUtils.isEmpty(hasFeatureList) && hasFeatureList.contains(benefitTaskDTO.getFeatureName());
        if (StringUtils.isEmpty(benefitTaskDTO.getFeatureName()) || hasFeature) {
            return true;
        }
        return false;
    }

    @Override
    public boolean checkRuleBefore(BenefitTaskDTO benefitTaskDTO) {
        return true;
    }

    @Override
    protected AbstractRuleHandler getNextRuleHandle() {
        return ruleHandler;
    }
}

 

posted @ 2022-04-07 16:42  Antony_hubei  阅读(53)  评论(0编辑  收藏  举报