1

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

所以当我打电话给建设者时:

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

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

首先,工厂建设者是谨慎的做法吗?其次,我的建造者是否可以挽救?

生成器:

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

编辑/固定:

因此,对于它的价值,这里是修改后的构建器。它需要另一个可以正确初始化它应该构建的实例的构造函数。

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;
    }
}
4

2 回答 2

2

每次您只需覆盖中的值

private String txtFieldName;
private String txtFieldValue;

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

ValidationHelper v = new ValidationHelper.HelperBuilder()

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

private ArrayList<HelperBuilder> innerValidatorHelpers = ...;
于 2012-08-13T10:39:13.967 回答
2

如果要构建具有不同值的对象,则必须

  • 在创建对象之间更改构建器,以便构建不同的东西。
  • 指示构建器自动更改值,例如使用计数器或基于日期的文件名,或提供值列表。
于 2012-08-13T10:39:16.443 回答