如何将构建器模式与所有参数强制一起使用?

joh*_*ohn 0 java design-patterns builder builder-pattern

我有一个构建器模式,在该模式中,很可能所有参数都是必需的,因此我创建了一个长的构造函数,如下代码所示。

public final class ResponseHolder {

    // all below six are related to response information
    private final String response;
    private final boolean isLinking;
    private final TypeHold typeOfId;
    private final long userTimeInMs;
    private final long userLmdInDays;
    private final String maskInfo;

    // below two are related to error handling
    private final ErrorCode error;
    private final StatusCode status;

    private ResponseHolder(Builder builder) {
        this.response = builder.response;
        this.isLinking = builder.isLinking;
        this.typeOfId = builder.typeOfId;
        this.userTimeInMs = builder.userTimeInMs;
        this.userLmdInDays = builder.userLmdInDays;
        this.maskInfo = builder.maskInfo;
        this.error = builder.error;
        this.status = builder.status;
    }

    public static class Builder {
        protected final String response;
        protected final TypeHold typeOfId;
        protected final String maskInfo;
        protected final ErrorCode error;
        protected final StatusCode status;
        protected final boolean isLinking;
        protected final long userTimeInMs;
        protected final long userLmdInDays;


        public Builder(String response, TypeHold typeOfId, String maskInfo, ErrorCode error,
                StatusCode status, boolean isLinking, long userTimeInMs, long userLmdInDays) {
            this.response = response;
            this.typeOfId = typeOfId;
            this.maskInfo = maskInfo;
            this.error = error;
            this.status = status;
            this.isLinking = isLinking;
            this.userTimeInMs = userTimeInMs;
            this.userLmdInDays = userLmdInDays
        }

        public ResponseHolder build() {
            return new ResponseHolder(this);
        }
    }

    // getters here
}
Run Code Online (Sandbox Code Playgroud)

现在,当所有参数都是强制性的时,我会感到困惑,那么如何使用它呢?有没有更好的方式来表示我上面的Builder模式?可能在逻辑上将传递给它们自己的类的参数分组,以减少传递给构建器构造函数的参数数量?

尽管拥有单独的对象可以使事情大大简化,但是如果不熟悉代码,也会使事情变得有些困难。我可以做的一件事是将所有参数移到它们自己的addParam(param)方法中,然后build()在运行时对方法中的所需参数进行验证?

我在这里应该遵循的最佳实践是什么,在这里可以使用更好的方法吗?

And*_*eas 6

构建器模式的目的是拥有一个无参数构造函数、许多命名良好的 setter 方法和一个最终完成方法,该方法在构造目标对象之前验证给定值的组合是否有效。

在您的情况下,由于需要所有值,因此构建器模式的主要目的是提供一种命名参数支持形式。

此外,您的目标对象应该具有多参数构造函数,而不是将构建器对象作为参数)

因此,您的构建器应该是(假设不允许空值):

public static class Builder {
    private String     response;
    private TypeHold   typeOfId;
    private String     maskInfo;
    private ErrorCode  error;
    private StatusCode status;
    private Boolean    isLinking;
    private Long       userTimeInMs;
    private Long       userLmdInDays;

    public Builder setResponse(String response) {
        this.response = response;
        return this;
    }
    public Builder setTypeOfId(TypeHold typeOfId) {
        this.typeOfId = typeOfId;
        return this;
    }
    public Builder setMaskInfo(String maskInfo) {
        this.maskInfo = maskInfo;
        return this;
    }
    public Builder setError(ErrorCode error) {
        this.error = error;
        return this;
    }
    public Builder setStatus(StatusCode status) {
        this.status = status;
        return this;
    }
    public Builder setIsLinking(boolean isLinking) {
        this.isLinking = isLinking;
        return this;
    }
    public Builder setUserTimeInMs(long userTimeInMs) {
        this.userTimeInMs = userTimeInMs;
        return this;
    }
    public Builder setUserLmdInDays(long userLmdInDays) {
        this.userLmdInDays = userLmdInDays;
        return this;
    }
    public ResponseHolder build() {
        if (this.response == null ||
            this.typeOfId == null ||
            this.maskInfo == null ||
            this.error == null ||
            this.status == null ||
            this.isLinking == null ||
            this.userTimeInMs == null ||
            this.userLmdInDays == null) {
            throw new IllegalStateException("Not all required values given");
        }
        return new ResponseHolder(this.response,
                                  this.typeOfId,
                                  this.maskInfo,
                                  this.error,
                                  this.status,
                                  this.isLinking,
                                  this.userTimeInMs,
                                  this.userLmdInDays);
    }
}
Run Code Online (Sandbox Code Playgroud)

您现在可以像这样使用它:

ResponseHolder holder = new ResponseHolder.Builder()
                        .setResponse(response)
                        .setTypeOfId(typeOfId)
                        .setMaskInfo(maskInfo)
                        .setError(error)
                        .setStatus(status)
                        .setIsLinking(isLinking)
                        .setUserTimeInMs(userTimeInMs)
                        .setUserLmdInDays(userLmdInDays)
                        .build();
Run Code Online (Sandbox Code Playgroud)


sst*_*tan 5

当有许多不同的有效参数排列允许您创建对象时,Builder模式就会发光。如果没有Builder模式,您将不得不创建许多丑陋且令人困惑的构造函数来处理所有可能的有效参数组合。

但是在您的情况下,只有一组有效的参数允许您创建对象。这正是构造函数的用途。在这里使用Builder模式不仅过分,而且根本不合适。

只需为您的ResponseHolder类使用普通的构造函数即可。

  • 具有多个参数的构造函数不必看上去难看,这取决于您的代码格式。当时我的建议是将第一个参数与构造函数放在同一行,然后将其下的其余参数对齐。这不仅可以提高可读性(因为您可以在每个部分后面进行注释),而且可以简化代码,因为您不必在类中添加额外的“生成器”逻辑/代码等,并且最终用户可以使用自动完成功能他们的IDE以帮助完成构造函数。) (2认同)