实际在项目中用到的,一个简单的场景。

因为这个简单的修改,把原来很糟糕的代码进行了很好的重构。

好处是数据库领域模型不需要知道实际是什么组件的配置,只需要引用父类。实际这个组件配置的校验自需要放在校验层,起到解耦合的作用。

配置的父类

public class ConfigureDefinition implements Serializable {
    private final static Logger LOGGER = LoggerFactory.getLogger(ConfigureDefinition.class);
    public static String valicateErrorMessage;

    public TaskNodeDefinition addConfiguration(Object json,TaskNodeDefinition target){
        LOGGER.info("--->ConfigureDefinition addConfiguration() the [task node]<taskNode:{}>", target.toString());
        return target;
    }

    public TaskNodeDefinition processConfiguration(TaskNodeDefinition taskNodeDefinition) throws Exception{
        LOGGER.info("--->ConfigureDefinition processConfiguration() the [task node]<taskNode:{}>", taskNodeDefinition.toString());
        validate(taskNodeDefinition);
        return taskNodeDefinition;
    }

    public boolean validate(TaskNodeDefinition taskNodeDefinition) {
        LOGGER.info("--->ConfigureDefinition validate() the [task node]<taskNode:{}>", taskNodeDefinition.toString());
        return true;
    }

    public String validateReturnMessage(TaskNodeDefinition taskNodeDefinition){
        LOGGER.info("--->ConfigureDefinition validateReturnMessage() the [task node]<taskNode:{}>", taskNodeDefinition.toString());
        validate(taskNodeDefinition);
        return valicateErrorMessage;
    }

}

不同的子类有各自的业务逻辑,如果没有就使用父类的逻辑

public class HttpRequestConfig extends ConfigureDefinition {
    private final static Logger LOGGER = LoggerFactory.getLogger(HttpRequestConfig.class);


    @Override
    public TaskNodeDefinition addConfiguration(Object json,TaskNodeDefinition target){
        LOGGER.info("---> HttpRequestConfig addConfiguration() the [HTTP Request]<taskNode:{}> <{}>", target.getTaskId(),json);
        JSONObject jsonObject= JSONObject.fromObject(json);
        HttpRequestDefinition httpRequestDefinition=(HttpRequestDefinition)JSONObject.toBean(jsonObject, HttpRequestDefinition.class);
        target.setConfigure(httpRequestDefinition);
        return target;
    }

    @Override
    public TaskNodeDefinition processConfiguration(TaskNodeDefinition taskNodeDefinition) throws Exception {
        return super.processConfiguration(taskNodeDefinition);
    }

    @Override
    public boolean validate(TaskNodeDefinition taskNodeDefinition) {
        LOGGER.info("--->HttpRequestConfig validate() the [HTTP Request]<taskNode:{}>", taskNodeDefinition.toString());
        if (Objects.isNull(taskNodeDefinition.getConfigure())
                || Objects.isNull(taskNodeDefinition.getConfigure())
                || Objects.isNull(((HttpRequestDefinition)taskNodeDefinition.getConfigure()).getGeneral())
                || StringUtils.isAnyBlank(((HttpRequestDefinition)taskNodeDefinition.getConfigure()).getGeneral().getMethod(),
                ((HttpRequestDefinition)taskNodeDefinition.getConfigure()).getGeneral().getPath(),
                ((HttpRequestDefinition)taskNodeDefinition.getConfigure()).getGeneral().getAccessKeyId())) {
            valicateErrorMessage="You must specify a value in the following fields: method、path、accessKeyId";
            return false;
        }
        valicateErrorMessage=null;
        return true;
    }

    @Override
    public String validateReturnMessage(TaskNodeDefinition taskNodeDefinition){
        LOGGER.info("--->HttpRequestConfig validateReturnMessage() the [HTTP Request]<taskNode:{}>", taskNodeDefinition.toString());
        validate(taskNodeDefinition);
        return valicateErrorMessage;
    }

数据库层领域模型

public class HttpRequestDefinition extends HttpRequestConfig implements Serializable {
    private GeneralDefinition general;
    private RequestDefinition request;
    private ResponseDefinition response;

    public GeneralDefinition getGeneral() {
        return general;
    }

    public void setGeneral(GeneralDefinition general) {
        this.general = general;
    }

    public RequestDefinition getRequest() {
        return request;
    }

    public void setRequest(RequestDefinition request) {
        this.request = request;
    }

    public ResponseDefinition getResponse() {
        return response;
    }

    public void setResponse(ResponseDefinition response) {
        this.response = response;
    }
}