我有两种来自上游的有效负载:PayloadA
或PayloadB
。与PayloadA
相比,PayloadB
中包含许多字段,但是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);
}
}
并在
DataProcessor
process
方法中: 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?
}
现在如何在
createTimestamp
方法的key
类中设置Payload
和process
变量?现在,我有一个区分的运行方法,但通常我将为PayloadA使用不同的上游代码,为PayloadB使用不同的上游代码,因此我们将使用Payload类之一。另外,我应该在这里安排两名不同的建筑商,还是在这里一名大型建筑商?
最佳答案
PayloadA
和PayloadB
可以扩展Payload
,如下所示:
public abstract class Payload {
private long createTimestamp;
private String key;
// some other fields are here
// getters and setters here
}
public class PayloadA extends Payload {
//add existing code
}
public class PayloadB extends Payload {
//add existing code
}
private void process(Payload payload) {
//Depending upon the object passed, fields will be set for A or B
payload.setCreateTimestamp(ADD_DATA1);
payload.setKey(ADD_DATA2);
//set other fields
//if(payload instanceof PayloadA) {
//payloadA
//}
}
如何确定有效载荷是
PayloadA
还是PayloadB
在
process()
里面?您可以找到使用
instanceof
之类的内容,例如payload
instanceof
PayloadA
,如上所示。但是,通常来说,使用instanceof
检查进行编码不是一个好主意,因此,除非无法避免,否则不要使用它。我应该在这里有两个不同的建筑商还是在这里一个大建筑商
做任何事情?
根据上面提供的代码,这些字段在
PayloadA
和PayloadB
上有很大的不同,因此最好保留单独的bean和相应的构建器。更新:我需要弄清楚它是什么类型的有效载荷,并基于
我需要为键变量设置值吗?
在
setKey()
内部将对传递给process(Payload payload)
的对象类型(多态性,OOP的基本原理之一)进行调用,即,如果从PayloadA
方法传递run()
对象,则在setKey()
上传递PayloadA
对象将被调用。总而言之,您根本不需要instanceof
检查。由您决定要在哪里设置密钥,可以在process()
方法内(如果您还有其他依赖项来生成key
),也可以按照@Roberto的建议进行操作关于java - 在构建器模式中将抽象类子类化?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41005851/