Is it wrong to have a lot of parameters inside a constructor? Like 10 to 15 parameters? Because I was designing a class where a constructor will have lots of parameters, for
Instead of using telescoping constructor pattern, use builder pattern
public class Person {
private final String fName;
private final String lName;
private final String mInitial;
private final int age;
private final String contactNumber;
private final String emailAddress;
public Person(PersonBuilder builder) {
//insert rest of code here
fName = builder.fName;
...
}
public static class PersonBuilder {
private String fName;
private String lName;
private String mInitial;
private int age;
private String contactNumber;
private String emailAddress;
// setter methods
public PersonBuilder setFirstName(String name) {
fName = name;
return this;
}
...
// build method
public Person build() {
return new Person(this);
}
}
}
...
Person p = new PersonBuilder()
.setFirstName("")
// set all the setter methods
.build();