【发布时间】:2016-12-18 07:20:33
【问题描述】:
我有一个构建器类,我在我的一个项目中使用它。
- 假设我有
metricA作为基于以下类的构建器。 - 我需要在
metricA的基础上通过克隆metricA来创建一个新的构建器metricB,以便metricB包含metricA中已经存在的所有值。
在MetricHolder的构造函数中,我正在根据已经设置的字段初始化一些字段(不是直接设置的)。
-
clientTypeOrPayId- 我正在初始化这个字段。如果payId存在,那么我将设置此值或设置clientType。 -
clientKey- 我也在同一个构造函数中初始化这个字段。 - 最重要的是,我在
clientPayload映射中添加了一些必填字段。我不确定这样做的正确方法是什么。但我需要将is_clientid和is_deviceid添加到地图中。 (总的来说,我会添加更多字段)。 - 然后在最后一个构造函数中,我正在计算延迟差异并将其发送到其他系统。
下面是我的课:
public final class MetricHolder {
private final String clientId;
private final String deviceId;
private final String payId;
private final String clientType;
private final String clientTypeOrPayId;
private final Schema schema;
private final String schemaId;
private final String clientKey;
private final Map<String, String> clientPayload;
private final Record record;
private final long clientCreateTimestamp;
private final long clientSentTimestamp;
private MetricHolder(Builder builder) {
this.payId = builder.payId;
this.siteId = builder.siteId;
this.clientType = builder.clientType;
this.clientId = builder.clientId;
this.deviceId = builder.deviceId;
this.schema = builder.schema;
this.schemaId = builder.schemaId;
// populating all the required fields in the map and make it immutable
// not sure whether this is right?
builder.clientPayload.put("is_clientid", (clientId == null) ? "false" : "true");
builder.clientPayload.put("is_deviceid", (clientId == null) ? "true" : "false");
this.clientPayload = Collections.unmodifiableMap(builder.clientPayload);
this.clientTypeOrPayId = Strings.isNullOrEmpty(payId) ? clientType : payId;
this.record = builder.record;
this.clientKey = "process:" + System.currentTimeMillis() + ":"
+ ((clientId == null) ? deviceId : clientId);
this.clientCreateTimestamp = builder.clientCreateTimestamp;
this.clientSentTimestamp = builder.clientSentTimestamp;
// this will be called twice while cloning
// what is the right way to do this then?
SendData.getInstance().insert(clientTypeOrPayId,
System.currentTimeMillis() - clientCreateTimestamp);
SendData.getInstance().insert(clientTypeOrPayId,
System.currentTimeMillis() - clientSentTimestamp);
}
public static class Builder {
private final Record record;
private Schema schema;
private String schemaId;
private String clientId;
private String deviceId;
private String payId;
private String clientType;
private Map<String, String> clientPayload;
private long clientCreateTimestamp;
private long clientSentTimestamp;
// this is for cloning
public Builder(MetricHolder packet) {
this.record = packet.record;
this.schema = packet.schema;
this.schemaId = packet.schemaId;
this.clientId = packet.clientId;
this.deviceId = packet.deviceId;
this.payId = packet.payId;
this.clientType = packet.clientType;
// make a new map and check whether mandatory fields are present already or not
// and if they are present don't add it again.
this.clientPayload = new HashMap<>();
for (Map.Entry<String, String> entry : packet.clientPayload.entrySet()) {
if (!("is_clientid".equals(entry.getKey()) || "is_deviceid".equals(entry.getKey())) {
this.clientPayload.put(entry.getKey(), entry.getValue());
}
}
this.clientCreateTimestamp = packet.clientCreateTimestamp;
this.clientSentTimestamp = packet.clientSentTimestamp;
}
public Builder(Record record) {
this.record = record;
}
public Builder setSchema(Schema schema) {
this.schema = schema;
return this;
}
public Builder setSchemaId(String schemaId) {
this.schemaId = schemaId;
return this;
}
public Builder setClientId(String clientId) {
this.clientId = clientId;
return this;
}
public Builder setDeviceId(String deviceId) {
this.deviceId = deviceId;
return this;
}
public Builder setPayId(String payId) {
this.payId = payId;
return this;
}
public Builder setClientType(String clientType) {
this.clientType = clientType;
return this;
}
public Builder setClientPayload(Map<String, String> payload) {
this.clientPayload = payload;
return this;
}
public Builder setClientCreateTimestamp(long clientCreateTimestamp) {
this.clientCreateTimestamp = clientCreateTimestamp;
return this;
}
public Builder setClientSentTimestamp(long clientSentTimestamp) {
this.clientSentTimestamp = clientSentTimestamp;
return this;
}
public MetricHolder build() {
return new MetricHolder(this);
}
}
// getters
}
问题:-
以下是我如何制作metricA builder 对象:
MetricHolder metricA = new MetricHolder.Builder(record).setClientId("123456").setDeviceId("abcdefhg")
. setPayId("98765").setClientPayload(payloadMapHolder).setClientCreateTimestamp(createTimestamp)
.setClientSentTimestamp(sentTimestamp).build();
这就是我稍后在获取所有其他字段时在代码中克隆 metricA 对象的方式,如下所示:
MetricHolder metricB = new MetricHolder.Builder(metricA).setSchema(schema).setSchemaId("345").build();
我现在看到两个问题:
- 首先,
MetricHolder构造函数中的SendData.getInstance()行将被调用两次。首先是当我制作metricA时,其次是当我通过克隆metricA制作metricB时。但是当我尝试创建metricA构建器对象时,我只想调用它一次?我怎样才能做到这一点? - 其次,我在
MetricHolder构造函数中使用两个必填字段填充clientPayload映射的方式对我来说看起来不正确。有没有其他更好的方法来做同样的事情?
我想整个问题正在发生,因为我克隆metricA 以创建metricB 构建器对象的方式?做这个的最好方式是什么?我想以正确的方式实现上述两件事。
【问题讨论】:
-
你的问题很不清楚,因为里面有很多不必要的东西(很多字段和设置器等看起来不相关)。请您将其剥离为minimal reproducible example,以便您的代码的关键部分更加明显?
标签: java clone builder builder-pattern