【发布时间】:2016-12-06 22:07:59
【问题描述】:
我有两种来自上游的有效负载:PayloadA 或 PayloadB。与PayloadB 相比,PayloadA 有很多字段,但PayloadA 和PayloadB 之间有一些共同字段。为了让示例更简单,我只添加了几个字段。
下面是PayloadA 的构建器类:
public final class PayloadA {
private final String clientId;
private final String langid;
private final String deviceId;
private final Map<String, String> applicationPayload;
// other fields as well
private PayloadA(Builder builder) {
this.clientId = builder.clientId;
this.langid = builder.langid;
this.deviceId = builder.deviceId;
this.applicationPayload = builder.applicationPayload.build();
}
public static class Builder {
protected final String deviceId;
protected String clientId;
protected String langid;
protected ImmutableMap.Builder<String, String> applicationPayload = ImmutableMap.builder();
public Builder(String deviceId) {
this.deviceId = deviceId;
}
public Builder setClientId(String clientId) {
this.clientId = clientId;
return this;
}
public Builder setLangid(String langid) {
this.langid = langid;
return this;
}
public Builder setPayload(Map<String, String> payload) {
this.applicationPayload.putAll(payload);
return this;
}
public PayloadA build() {
return new PayloadA(this);
}
}
// getters and to string here
}
下面是PayloadB的类:
public final class PayloadB {
private final String clientid;
private final String type;
private final String payId;
// other fields as well
private PayloadB(Builder builder) {
this.clientid = builder.clientid;
this.type = builder.type;
this.payId = builder.payId;
}
public static class Builder {
protected final String type;
protected String payId;
protected String clientid;
public Builder(String type) {
this.type = type;
}
public Builder setPayId(String payId) {
this.payId = payId;
return this;
}
public Builder setClientId(String clientid) {
this.clientid = clientid;
return this;
}
public PayloadB build() {
return new PayloadB(this);
}
}
// getters and to string here
}
现在我创建了另一个类 Payload 类,其中我有 PayloadA 和 PayloadB 的所有公共字段,所以我必须以某种方式设置这些字段,我不知道如何使用下课:
public abstract class Payload {
private long createTimestamp;
private String key;
// some other fields are here
// getters and setters here
}
问题:
现在从下面的代码中,我根据传递的内容制作PayloadA 或PayloadB。
private void run(String name) {
// .. some code here
if (name.equalsIgnoreCase("PayloadA")) {
Payload payload =
new PayloadA.Builder(getDeviceId()).setClientId("someid").setLangid("anotherid")
.setPayload("some map").build();
DataProcessor.getInstance().process(payload);
} else {
Payload payload =
new PayloadB.Builder(getType()).setPayId("someid").setClientId("anotherid").build();
DataProcessor.getInstance().process(payload);
}
}
在DataProcessorprocess方法中:
private void process(Payload payload) {
// 1) here I need to set createTimestamp and key variables on payload bcoz they are common
// fields.
// 2) Also how can I figure out whether payload is PayloadA or PayloadB here?
}
现在如何在process 方法中设置Payload 类中的createTimestamp 和key 变量?现在我有一个运行方法,我正在区分它,但总的来说,我将为 PayloadA 提供不同的上游代码,为 PayloadB 提供不同的上游代码,因此我们将使用任一 Payload 类。
我还应该在这里有两个不同的建设者还是一个大建设者来做所有事情?
【问题讨论】:
标签: java design-patterns fluent builder-pattern