I have two types of payload coming from upstream: It's either PayloadA
or PayloadB
. There are some common fields between PayloadA
and PayloadB
so I created Payload
class with those common fields and for rest I created two builder class one for each payload. In general they will have lot of fields.
Below is the builder class for PayloadA
:
public final class PayloadA {
private final String clientId;
private final String langid;
private final String deviceId;
private final Map<String, String> applicationPayload;
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
}
Now below is the class for PayloadB
:
public final class PayloadB {
private final String clientid;
private final String type;
private final String payId;
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
}
Now I have created another class which is Payload
class (does this have to be abstract class?) in which I have all the common fields both for PayloadA
and PayloadB
so I have to set these fields as well somehow and I am not sure how to use below class:
public abstract class Payload {
private long createTimestamp;
private String key;
// getters and setters here
}
Question:
Now from the below code I am making either PayloadA or PayloadB depending on what is passed.
protected void run(String name) {
// .. some code here
if(name.equalsIgnoreCase("PayloadA") {
PayloadA payload = new PayloadA.Builder(getDeviceId())
.setClientId("someid")
.setLangid("anotherid")
.setPayload("some map")
.build();
DataProcessor.getInstance().process(payload);
} else {
PayloadB payload = new PayloadB.Builder(getType())
.setPayId("someid")
.setClientId("anotherid")
.build();
DataProcessor.getInstance().process(payload);
}
// .. some code here
}
Now how can I set createTimestamp
and key
variable which is in Payload class? I have to set createTimestamp
in the process
method of DataProcessor
. And key
variable is determine from few fields either from PayloadA or PayloadB depending on what is set.
Should I have on big builder pattern class with everything in it or multiple builder pattern extending something like Payload class (which has common fields)?
Aucun commentaire:
Enregistrer un commentaire