gpt4 book ai didi

java - 静态工厂 builder 的麻烦

转载 作者:行者123 更新时间:2023-11-29 03:43:47 25 4
gpt4 key购买 nike

需要创建数量不详的对象,我尝试创建一个构建器来执行此操作。一切都很好,直到我意识到我的构建器创建的所有对象的属性都具有相同的值。

所以当我调用构建器时:

ValidationHelper v = new ValidationHelper.HelperBuilder()
.addHelper("ICAO Identifier", icaoIdentifier, rulesICAO)
.addHelper("Long Name", longName, rulesLongName)
.build();

...我将有 2 个对象,它们的属性将具有构建器被要求创建的最后一个对象的值。

首先,工厂 build 者是否是谨慎的做法?其次,我的构建器是否还有救?

builder :

public class ValidationHelper {

private static ArrayList<HelperBuilder> validatorHelpers = new ArrayList();


public static class HelperBuilder {

private String txtFieldName;
private String txtFieldValue;

private List<Integer> valCodes = new ArrayList<Integer>();

private ArrayList<HelperBuilder> innerValidatorHelpers = new ArrayList<HelperBuilder>();

public HelperBuilder() {}

public final HelperBuilder addHelper(String txtFieldName, String txtFieldValue, int[] validationCodes) {

this.txtFieldName = txtFieldName;
this.txtFieldValue = txtFieldValue;
for( int i = 0; i < validationCodes.length; i++ ){
getValCodes().add((Integer) validationCodes[i]);
}

innerValidatorHelpers.add(this);
return this;
}

public final ValidationHelper build() {
return new ValidationHelper(this);
}

public String getTxtFieldName() {
return txtFieldName;
}

public String getTxtFieldValue() {
return txtFieldValue;
}

public List<Integer> getValCodes() {
return valCodes;
}
}//end HelperBuilder

private ValidationHelper(HelperBuilder helperBuilder) {
validatorHelpers = helperBuilder.innerValidatorHelpers;
}

public void setHelpers(ArrayList validatorHelpers) {
validatorHelpers = validatorHelpers;
}

public ArrayList getHelpers() {
return validatorHelpers;
}
}

编辑/修复:

因此,就其值(value)而言,这是修改后的构建器。它需要另一个构造函数来正确初始化它应该构建的实例。

public class ValidationHelper {

private static ArrayList<HelperBuilder> validatorHelpers = new ArrayList();

public static class HelperBuilder {

private String txtFieldName;
private String txtFieldValue;

private List<Integer> valCodes = new ArrayList<Integer>();

private ArrayList<HelperBuilder> innerValidatorHelpers = new ArrayList<HelperBuilder>();

public HelperBuilder() {}
public HelperBuilder(String txtFieldName, String txtFieldValue, int[] validationCodes) {

this.txtFieldName = txtFieldName;
this.txtFieldValue = txtFieldValue;

for (int i = 0; i < validationCodes.length; i++) {
valCodes.add((Integer) validationCodes[i]);
}

}

public final HelperBuilder addHelper(String txtFieldName, String txtFieldValue, int[] validationCodes) {
innerValidatorHelpers.add( new HelperBuilder(txtFieldName, txtFieldValue, validationCodes) );
return this;
}

public final ValidationHelper build() {
return new ValidationHelper(this);
}

public String getTxtFieldName() {
return txtFieldName;
}

public String getTxtFieldValue() {
return txtFieldValue;
}

public List getValCodes() {
return valCodes;
}
}//end HelperBuilder

private ValidationHelper(HelperBuilder helperBuilder) {
validatorHelpers = helperBuilder.innerValidatorHelpers;
}

public ArrayList getHelpers() {
return validatorHelpers;
}
}

最佳答案

每次你只是覆盖中的值

private String txtFieldName;
private String txtFieldValue;

最后一个获胜。所以你在这里只创建 1 个 HelperInstance

ValidationHelper v = new ValidationHelper.HelperBuilder()

每次调用 addHelper() 时,字段名称和值都会被覆盖。但是您需要为每个“配置”创建一个实例。所以 addHelper 应该创建一个新的 Instance 并将其添加到

private ArrayList<HelperBuilder> innerValidatorHelpers = ...;

关于java - 静态工厂 builder 的麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11932623/

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