gpt4 book ai didi

java - 在构建器模式中子类化抽象类?

转载 作者:行者123 更新时间:2023-12-02 03:11:14 25 4
gpt4 key购买 nike

我有两种来自上游的有效负载:它是 PayloadAPayloadB。与 PayloadB 相比,PayloadA 中有很多字段,但 PayloadAPayloadB 之间有一些公共(public)字段。为了使示例更简单,我只添加了几个字段。

下面是 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 类,其中我拥有 PayloadAPayloadB 的所有公共(public)字段,所以我有以某种方式设置这些字段,我不知道如何使用下面的类:

public abstract class Payload {
private long createTimestamp;
private String key;
// some other fields are here

// getters and setters here
}

问题:

现在,根据下面的代码,我将根据传递的内容制作 PayloadAPayloadB

  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?
}

现在如何在 process 方法中设置 createTimestampkey 变量(位于 Payload 类中)?现在我有一个 run 方法,我在其中区分它,但一般来说,我将为 PayloadA 提供不同的上游代码,为 PayloadB 提供不同的上游代码,因此根据情况,我们将使用任一 Payload 类。

此外,我应该在这里有两个不同的构建器还是一个大型构建器在这里做所有事情?

最佳答案

PayloadAPayloadB 可以扩展 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
//}
}

How can I figure out whether payload is PayloadA or PayloadB inside process()?

你可以发现使用instanceof就像payload instanceof PayloadA如上所示。但是,一般来说,使用 instanceof 检查进行编码并不是一个好主意,因此除非无法避免,否则不要使用它。

Should I have two different builders here or one big builder here doing everything?

根据您上面提供的代码,这些字段PayloadAPayloadB完全不同,因此最好保留单独的bean和各自的构建器。

UPDATE: I need to figure out what type of payload it is and basis on that I need to set values for key variable?

内部setKey()将在传递给process(Payload Payload)的对象类型上被调用(多态性,OOP的基本原则之一),即,如果您从 run() 方法传递 PayloadA 对象,PayloadA 对象上的 setKey() 将被调用。 总而言之,您根本不需要 instanceof 检查。这取决于您的要求,您想在哪里设置 Key,它可以在 内部process() 方法(如果您有一些其他依赖项来生成key),或者可以按照@Roberto

的建议来完成

关于java - 在构建器模式中子类化抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com