如何通过添加更多键来修改构建器模式?

时间:2014-06-29 05:04:29

标签: java enums builder

我正在开发一个项目,我需要通过向其传递不同的参数来创建一个键,所以我决定在这里使用Builder模式。

下面是我在调用代码之前需要创建的DataKey类构建器。在下面的构建器类中,我们有三个重要的键,userIduuiddeviceId

public final class DataKey {

    private final long userId;
    private final long uuid;
    private final long deviceid;
    private final int clientId;
    private final long timeout;

    private DataKey(Builder builder) {
        this.userId = builder.userId;
        this.uuid = builder.uuid;
        this.deviceid = builder.deviceid;
        this.clientId = builder.clientId;
        this.timeout = builder.timeout;
    }

    public static class Builder {
        protected final long userId;
        protected final long uuid;
        protected final long deviceid;
        protected final int clientId;
        protected long timeout = 200L;          

        public Builder setUserId(long userId) {
            this.userId = userId;
            return this;
        }

        public Builder setUuid(String uuid) {
            this.uuid = uuid;
            return this;
        }


        public Builder setDeviceId(String deviceid) {
            this.deviceid = deviceid;
            return this;
        }

        public Builder setClientId(String clientId) {
            this.clientId = clientId;
            return this;
        }

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

    public long getUserId() {
        return userId;
    }

    public long getUuid() {
        return uuid;
    }

    public long getDeviceId() {
        return deviceid;
    }

    public int getclientId() {
        return clientId;
    }

    public long getTimeout() {
        return timeout;
    }       
}

问题陈述: -

此处,客户将始终通过modelId,但他们可以向我们传递重要内容密钥的不同组合,例如 -

  • userId, uuid, deviceId:如果他们正在传递userIduuiddeviceId,则首选项将为userId。现在,我将使用userId,这意味着它是USERFLOW,如下面的枚举所示。
  • uuid, deviceId:如果他们正在传递uuiddeviceId,那么首选项将为uuid。现在,我将使用uuid,这意味着它是DEVICEFLOW,如下面的枚举所示。
  • deviceId:如果他们正在传递deviceId,则首选项将为deviceId,因为这是他们通过的唯一重要密钥。现在,我将使用deviceId,这意味着它是DEVICEFLOW,如下面的枚举所示。

简而言之,userId首先获得优先权,然后是uuid,然后是deviceId

他们只能通过clientId来致电我们,一些重要的密钥必须在那里,userIduuiddeviceId

userId is a long (which is USERFLOW)
uuid is a String (which is DEVICEFLOW)
deviceId is a String (which is DEVICEFLOW)

我也创建了一个枚举 -

public enum FlowEnum {
    USERFLOW, DEVICEFLOW
}

我还需要添加我们即将在DataKey课程中提供的枚举类型以及依赖于什么" id"获得优先顺序。由于FlowEnum对正在制作密钥的客户不透明。

以下是我可以想到人们会制作密钥的方法,但我如何确保他们从userId,uuid和deveiceid传递至少一个id密钥?

DataKey keys = new DataKey.Builder().setUserId(foo).setUuid(blaah).setDeviceId(foopes).setClientId(123).addTimeout(100L).build();

下面是我的简单Task类,它实现了Callable并完成了所有工作 -

class Task implements Callable<String> {

    private final DataKey keys;

    public Task(DataKey keys) {
        this.keys = keys;   
    }

    public String call() throws Exception {

        // print whether it is USERFLOW or DEVICEFLOW?
        // print which id keys I should use here?

    }   
}   

1 个答案:

答案 0 :(得分:1)

首先将您的枚举添加到DataKey

public final class DataKey {

    private long userId =0L;
    private long uuid =0L;
    private long deviceid =0L;
    private int clientId =0;
    private long timeout =0L;

    // add enum:
    private final FlowEnum flow;
    // then add getter etc...

现在更新构造函数:

private DataKey(Builder builder) {
    this.userId = builder.userId;
    this.uuid = builder.uuid;
    this.deviceid = builder.deviceid;
    this.clientId = builder.clientId;
    this.timeout = builder.timeout;

    if (userId == 0 && uuid == 0 && deviceId == 0) {
        throw new IllegalStateException("You have to pass at least one" +
          " of the following: userId, uuid or deviceId");
    }
    if (userId != 0) {
        this.flow = FlowEnum.USERFLOW;
    }
    else {
        this.flow = FlowEnum.DEVICEFLOW;
    }
}

然后在后面:

class Task implements Callable<String> {

    private final DataKey keys;

    public Task(DataKey keys) {
        this.keys = keys;   
    }

    public String call() throws Exception {

        // print whether it is USERFLOW or DEVICEFLOW? 
        System.out.println(keys.getFlow());

    }   
}