我最近在我的一个项目中开始使用Builder模式,我正在尝试在Builder类中添加某种验证。我假设我们不能在编译时这样做,这就是我在运行时进行此验证的原因。但可能是我错了,这就是我想看看我是否可以在编译时这样做。
传统构建器模式
public final class RequestKey {
private final Long userid;
private final String deviceid;
private final String flowid;
private final int clientid;
private final long timeout;
private final boolean abcFlag;
private final boolean defFlag;
private final Map<String, String> baseMap;
private RequestKey(Builder builder) {
this.userid = builder.userid;
this.deviceid = builder.deviceid;
this.flowid = builder.flowid;
this.clientid = builder.clientid;
this.abcFlag = builder.abcFlag;
this.defFlag = builder.defFlag;
this.baseMap = builder.baseMap.build();
this.timeout = builder.timeout;
}
public static class Builder {
protected final int clientid;
protected Long userid = null;
protected String deviceid = null;
protected String flowid = null;
protected long timeout = 200L;
protected boolean abcFlag = false;
protected boolean defFlag = true;
protected ImmutableMap.Builder<String, String> baseMap = ImmutableMap.builder();
public Builder(int clientid) {
checkArgument(clientid > 0, "clientid must not be negative or zero");
this.clientid = clientid;
}
public Builder setUserId(long userid) {
checkArgument(userid > 0, "userid must not be negative or zero");
this.userid = Long.valueOf(userid);
return this;
}
public Builder setDeviceId(String deviceid) {
checkNotNull(deviceid, "deviceid cannot be null");
checkArgument(deviceid.length() > 0, "deviceid can't be an empty string");
this.deviceid = deviceid;
return this;
}
public Builder setFlowId(String flowid) {
checkNotNull(flowid, "flowid cannot be null");
checkArgument(flowid.length() > 0, "flowid can't be an empty string");
this.flowid = flowid;
return this;
}
public Builder baseMap(Map<String, String> baseMap) {
checkNotNull(baseMap, "baseMap cannot be null");
this.baseMap.putAll(baseMap);
return this;
}
public Builder abcFlag(boolean abcFlag) {
this.abcFlag = abcFlag;
return this;
}
public Builder defFlag(boolean defFlag) {
this.defFlag = defFlag;
return this;
}
public Builder addTimeout(long timeout) {
checkArgument(timeout > 0, "timeout must not be negative or zero");
this.timeout = timeout;
return this;
}
public RequestKey build() {
if (!this.isValid()) {
throw new IllegalStateException("You have to pass at least one"
+ " of the following: userid, flowid or deviceid");
}
return new RequestKey(this);
}
private boolean isValid() {
return !(TestUtils.isEmpty(userid) && TestUtils.isEmpty(flowid) && TestUtils.isEmpty(deviceid));
}
}
// getters here
}
问题陈述:
如您所见,我有各种参数,但只有一个参数clientId
是强制性的,其余参数是可选的。在上面的代码中,我需要设置userid
,flowid
或deviceid
。如果这三个都没有设置,那么我正在向IllegalStateException
抛出一条错误消息,如上面的代码中所示。如果设置了全部三个或两个,那么它很好,我在这三个或两个上做一些优先级逻辑来决定使用哪一个但是必须设置其中一个。
并非强制要求他们每次都会传递所有三个id,他们可以传递所有三个或有时两个或有时只传递一个,但条件是其中一个应该被设置。
我正在寻找的是 - 不是在运行时完成所有这些事情,我可以在编译时执行此操作并且不构建我的构建器模式,除非设置了这三个中的任何一个并且在编译时它应该告诉它缺少什么?
我发现了这个SO link,它确实谈到同样的事情,但不确定如何在我的场景中使用它?还有builder pattern with a twist和此SO question
答案 0 :(得分:2)
根据您的描述,我会使用您已经提到的解决方案: How to improve the builder pattern?和Builder pattern with a twist略有不同:
这两个解决方案基本上都是链接构建器:您调用Builder.create().firstMandatoryField()
将返回第二个必填字段的构建器实例,依此类推,直到您到达最后一个构建器,该构建器具有实际{{1}调用私有构造函数的方法。
在您的情况下,有些字段必须至少设置其中一个字段,这意味着您的第一个构建器将提供初始化它们的方法并返回第二个构建器的实例。在第二个构建器上,您可以设置所有字段(选项和强制字段)。
这是实现此目的的一个版本:
build
然后你可以用:
来调用它public final class RequestKey {
private final Long userid;
private final String deviceid;
private final String flowid;
private final int clientid;
private final long timeout;
private final boolean abcFlag;
private final boolean defFlag;
private final Map<String, String> baseMap;
private RequestKey(FinalBuilder builder) {
this.userid = builder.userid;
this.deviceid = builder.deviceid;
this.flowid = builder.flowid;
this.clientid = builder.clientid;
this.abcFlag = builder.abcFlag;
this.defFlag = builder.defFlag;
this.baseMap = builder.baseMap.build();
this.timeout = builder.timeout;
}
public static class Builder {
public Builder1 clientId(int clientid) {
checkArgument(clientid > 0, "clientid must not be negative or zero");
return new Builder1(clientid);
}
}
public static class Builder1 {
private final int clientid;
Builder1(int clientid){
this.clientid = clientid;
}
public FinalBuilder userId(long userid) {
checkArgument(userid > 0, "userid must not be negative or zero");
FinalBuilder builder = new FinalBuilder(clientid);
return builder.setUserId(userid);
}
public FinalBuilder deviceId(String deviceid) {
checkNotNull(deviceid, "deviceid cannot be null");
checkArgument(deviceid.length() > 0, "deviceid can't be an empty string");
FinalBuilder builder = new FinalBuilder(clientid);
return builder.setDeviceId(deviceid);
}
public FinalBuilder flowId(String flowid) {
checkNotNull(flowid, "flowid cannot be null");
checkArgument(flowid.length() > 0, "flowid can't be an empty string");
FinalBuilder builder = new FinalBuilder(clientid);
return builder.setFlowId(flowid);
}
}
public static class FinalBuilder {
private final int clientid;
private Long userid = null;
private String deviceid = null;
private String flowid = null;
private long timeout = 200L;
private boolean abcFlag = false;
private boolean defFlag = true;
private ImmutableMap.Builder<String, String> baseMap = ImmutableMap.builder();
FinalBuilder(int clientId) {
this.clientid = clientId;
}
FinalBuilder setUserId(long userid) {
this.userid = userid;
return this;
}
FinalBuilder setDeviceId(String deviceid) {
this.deviceid = deviceid;
return this;
}
FinalBuilder setFlowId(String flowid) {
this.flowid = flowid;
return this;
}
public FinalBuilder userId(long userid) {
checkArgument(userid > 0, "userid must not be negative or zero");
this.userid = Long.valueOf(userid);
this.userid = userid;
return this;
}
public FinalBuilder deviceId(String deviceid) {
checkNotNull(deviceid, "deviceid cannot be null");
checkArgument(deviceid.length() > 0, "deviceid can't be an empty string");
this.deviceid = deviceid;
return this;
}
public FinalBuilder flowId(String flowid) {
checkNotNull(flowid, "flowid cannot be null");
checkArgument(flowid.length() > 0, "flowid can't be an empty string");
this.flowid = flowid;
return this;
}
public FinalBuilder baseMap(Map<String, String> baseMap) {
checkNotNull(baseMap, "baseMap cannot be null");
this.baseMap.putAll(baseMap);
return this;
}
public FinalBuilder abcFlag(boolean abcFlag) {
this.abcFlag = abcFlag;
return this;
}
public FinalBuilder defFlag(boolean defFlag) {
this.defFlag = defFlag;
return this;
}
public FinalBuilder addTimeout(long timeout) {
checkArgument(timeout > 0, "timeout must not be negative or zero");
this.timeout = timeout;
return this;
}
public RequestKey build() {
return new RequestKey(this);
}
}
public static Builder create() {
return new Builder();
}
// getters here
}