Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8362085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:51:37+00:00 2026-06-09T11:51:37+00:00

Needing to create an unspecified number of objects, I tried to create a builder

  • 0

Needing to create an unspecified number of objects, I tried to create a builder that do that. All was well until I realized that my builder creates all objects with their properties having the same values.

So when I call the builder:

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

… I’ll have 2 objects and their properties will have values of the last object the builder was asked to create.

To start with, is factory builder the prudent approach to this? Secondly, is my builder salvageable?

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

EDIT/FIXED:

So for what it’s worth, here’s the revised builder. It needed another constructor that could properly initialize an instance of what it’s supposed to build.

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:51:39+00:00Added an answer on June 9, 2026 at 11:51 am

    Each time you just overwrite the values in

    private String txtFieldName;
    private String txtFieldValue;
    

    and the last one winns. So you create only 1 HelperInstance here

    ValidationHelper v = new ValidationHelper.HelperBuilder()
    

    and the fields name and value are overwritten each time you call addHelper(). But you need to create an instance for each “configuration”. So addHelper should create a new Instance and add it into

    private ArrayList<HelperBuilder> innerValidatorHelpers = ...;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a set of persistent objects that load their state
I'm needing to create a zip file containing documents that exist on the server.
I often find myself needing reference to an object that is several objects away,
I'm needing to write a program (C#) that will allow the user to create
I'm needing to create a UserControl, that has a portion of the control's background
I'm needing to create a function for IPv4 and v6 that I cause use
I am needing to create something like a lock timer(a little thing that just
I am needing to create a foreach statement that will run through and create
I'm needing to create 3 DIVs in a footer container DIV that are aligned
I am needing to create a web service, so the first thing I did

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.