Search code examples
validationinputconstructorbuildermultiparameter

Which way to create an object when inputting data?


I have class Person and I want to input data form the user like first name, age, sex, weight, etc. (I wantto use the Scanner class.)

How should I deal with the input data to create an object of class Person? I consider the following options and would like to know which one to use.

1) Create a Person object with a no-args constructor at the START and then - input ONE piece of data (eg first name) - use the Person’s SETTER to set this data in the aforementioned object - repeat the steps for the other pieces of data At the end send the object to a PersonDAO

Disadvantage: If I include a no-args constructor for the Person class, I kind of allow the creation of an ‘empty’ invalid Person object somewhere else in the program. (I’m not really sure if I should really worry about that.  )

2) Create a Person object with a multiparameter constructor at the END. - first input the pieces of data one by one and keep them in an array/a list/variables. - when done with input, extract the pieces of data from the array/list/variables and create a Person object by putting them in the Person’s constructor (one with many parameters) At the end send the object to a PersonDAO.

Disadvantage: If one of the arguments is invalid, I will learn about that rather late. The whole procedure looks more complicated.

Which option is better then? Maybe there is some other way of dealing with input data before sending it to DAO...

And I guess, the DAO will use the object's GETTERS to create a record in a database?


Solution

  • I suggest using Builder design pattern. It looks like:

    public final class Person {
        private String firstName;
        private String lastName;
        private int age;
    
        private Person() {}
    
        public String getFirstName() {
            return firstName;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public int getAge() {
            return age;
        }
    
        public static Builder newBuilder () {
            return new Person(). new Builder();
        }
    
        public class Builder {
            public Builder setFirstName(String firstName) {
                //validate concrete params here with RuntimeExceptions
                Person.this.firstName = firstName;
                return this;
            }
    
            public Builder setLastName(String lastName) {
                Person.this.lastName = lastName;
                return this;
            }
    
            public Builder setAge(int age) {
                Person.this.age = age;
                return this;
            }
    
            public Person build() {
                //validate filling of Person here
                return Person.this;
            }
    
        }
    }
    

    So your class :

    • could have any count of initialization parameters
    • validate that class is ready inside a build method
    • class become immutable
    • encapsulated constructor