简单策略模式

31 阅读2分钟

简单策略模式

public class AutomationRuleCMDController extends BaseController{

    private final AutomationRuleHandlerFactory automationRuleHandlerFactory;

    @PostMapping("/bid/create")
    @Operation(summary = "create period bid rule")
    public RestApiResponse<Long> createPeriodBidRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationRuleCreateForm form) {
        form.setType(AutomationRuleType.BID);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.BID)
                .createRule(form);
        return RestApiResponse.success(ruleId);
    }

    @PostMapping("/bid/update")
    @Operation(summary = "update period bid rule")
    public RestApiResponse<Long> updatePeriodBidRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationRuleUpdateForm form) {
        form.setType(AutomationRuleType.BID);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.BID)
                .updateRule(form);
        return RestApiResponse.success(ruleId);
    }

    @PostMapping("/budget/create")
    @Operation(summary = "create period budget rule")
    public RestApiResponse<Long> createPeriodBudgetRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationRuleCreateForm form) {
        form.setType(AutomationRuleType.BUDGET);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.BUDGET)
                .createRule(form);
        return RestApiResponse.success(ruleId);
    }

    @PostMapping("/budget/update")
    @Operation(summary = "update period budget rule")
    public RestApiResponse<Long> updatePeriodBudgetRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationRuleUpdateForm form) {
        form.setType(AutomationRuleType.BUDGET);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.BUDGET)
                .updateRule(form);
        return RestApiResponse.success(ruleId);
    }

    @PostMapping("/metric/create")
    @Operation(summary = "create metric bid rule")
    public RestApiResponse<Long> createMetricBidRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationMetricRuleCreateForm form) {
        form.setType(AutomationRuleType.METRICS);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.METRICS)
                .createRule(form);
        return RestApiResponse.success(ruleId);
    }

    @PostMapping("/metric/update")
    @Operation(summary = "update metric bid rule")
    public RestApiResponse<Long> updateMetricBidRule(
            @PathVariable String platformCode,
            @RequestBody @Validated AutomationMetricRuleUpdateForm form) {
        form.setType(AutomationRuleType.METRICS);
        Long ruleId = automationRuleHandlerFactory.getHandler(AutomationRuleType.METRICS)
                .updateRule(form);
        return RestApiResponse.success(ruleId);
    }
}
public interface AutomationRuleProcessorBiz<C extends AutomationRuleBaseForm,
        U extends AutomationRuleBaseForm> {

    /**
     * 获取支持的规则类型
     */
    AutomationRuleType getSupportedRuleType();

    /**
     * 创建规则
     */
    Long createRule(C createForm);

    /**
     * 更新规则
     */
    Long updateRule(U updateForm);

    /**
     * 复制规则
     */
    Long cloneRule(AutomationRuleCloneForm form);

    /**
     * 删除规则
     */
    Boolean deleteRule(AutomationRuleDeleteForm form);
}
@Component
@RequiredArgsConstructor
public class AutomationRuleHandlerFactory {

    // 使用双重映射存储处理器
    private final Map<AutomationRuleType, AutomationRuleProcessorBiz<?, ?>> handlerMap = new EnumMap<>(AutomationRuleType.class);


    // 注册所有处理器
    @Autowired
    public AutomationRuleHandlerFactory(List<AutomationRuleProcessorBiz<?, ?>> processors) {
        processors.forEach(processor ->
                handlerMap.put(processor.getSupportedRuleType(), processor));
    }
    /**
     * 类型安全获取处理器
     */
    @SuppressWarnings("unchecked")
    public <C extends AutomationRuleBaseForm, U extends AutomationRuleBaseForm>
    AutomationRuleProcessorBiz<C, U> getHandler(AutomationRuleType type) {
        AutomationRuleProcessorBiz<?, ?> processor = handlerMap.get(type);
        if (processor == null) {
            throw new BusinessException(ApiErrorCodeMeta.NOT_SUPPORT_RULE_TYPE);
        }
        return (AutomationRuleProcessorBiz<C, U>) processor;
    }
}
public abstract class CriteoAutomationRuleBaseBiz<C extends AutomationRuleBaseForm,
        U extends AutomationRuleBaseForm>
        implements AutomationRuleProcessorBiz<C, U> {

    protected void validateName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new IllegalArgumentException("name is required");
        }
    }

    protected void validateExist(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("id is required");
        }
    }

    protected void copyAutomationRule(Long id, String name) {

    }

    protected void deleteAutomationRule(Long id, Boolean resetBid) {

    }
}
@Slf4j
@Service
@RequiredArgsConstructor
public class CriteoBidAutomationRuleBiz extends CriteoAutomationRuleBaseBiz<AutomationRuleCreateForm, AutomationRuleUpdateForm> {

    @Override
    public AutomationRuleType getSupportedRuleType() {
        return AutomationRuleType.BID;
    }

    @Override
    public Long createRule(AutomationRuleCreateForm form) {
        log.info("create budget automation rule, form: {}", form);
        return 0L;
    }

    @Override
    public Long updateRule(AutomationRuleUpdateForm form) {
        log.info("update budget automation rule, form: {}", form);
        return 0L;
    }

    @Override
    public Long cloneRule(AutomationRuleCloneForm form) {
        log.info("clone budget automation rule, form: {}", form);
        validateExist(form.getId());
        copyAutomationRule(form.getId(), form.getName());
        return 0L;
    }

    @Override
    public Boolean deleteRule(AutomationRuleDeleteForm form) {
        log.info("delete budget automation rule, form: {}", form);
        validateExist(form.getId());
        deleteAutomationRule(form.getId(), form.getResetBid());
        return true;
    }

}
@Slf4j
@Service
@RequiredArgsConstructor
public class CriteoBudgetAutomationRuleBiz extends CriteoAutomationRuleBaseBiz<AutomationRuleCreateForm, AutomationRuleUpdateForm> {

    private final AutomationRuleService automationRuleService;

    @Override
    public AutomationRuleType getSupportedRuleType() {
        return AutomationRuleType.BUDGET;
    }

    @Override
    public Long createRule(AutomationRuleCreateForm form) {
        log.info("create budget automation rule, form: {}", form);
        validateName(form.getName());
        return 0L;
    }

    @Override
    public Long updateRule(AutomationRuleUpdateForm form) {
        log.info("update budget automation rule, form: {}", form);
        validateExist(form.getId());
        return 0L;
    }

    @Override
    public Long cloneRule(AutomationRuleCloneForm form) {
        log.info("clone budget automation rule, form: {}", form);
        validateExist(form.getId());
        copyAutomationRule(form.getId(), form.getName());
        return 0L;
    }

    @Override
    public Boolean deleteRule(AutomationRuleDeleteForm form) {
        log.info("delete budget automation rule, form: {}", form);
        validateExist(form.getId());
        deleteAutomationRule(form.getId(), form.getResetBid());
        return true;
    }

}
@Slf4j
@Service
@RequiredArgsConstructor
public class CriteoMetricsAutomationRuleBiz extends CriteoAutomationRuleBaseBiz<AutomationRuleCreateForm, AutomationRuleUpdateForm> {

    @Override
    public AutomationRuleType getSupportedRuleType() {
        return AutomationRuleType.METRICS;
    }

    @Override
    public Long createRule(AutomationRuleCreateForm form) {
        log.info("create budget automation rule, form: {}", form);
        return 0L;
    }

    @Override
    public Long updateRule(AutomationRuleUpdateForm form) {
        log.info("update budget automation rule, form: {}", form);
        return 0L;
    }

    @Override
    public Long cloneRule(AutomationRuleCloneForm form) {
        log.info("clone budget automation rule, form: {}", form);
        validateExist(form.getId());
        copyAutomationRule(form.getId(), form.getName());
        return 0L;
    }

    @Override
    public Boolean deleteRule(AutomationRuleDeleteForm form) {
        log.info("delete budget automation rule, form: {}", form);
        validateExist(form.getId());
        deleteAutomationRule(form.getId(), form.getResetBid());
        return true;
    }

}