From the course: Java Design Patterns: Creational

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Avoid complex constructors

Avoid complex constructors - Java Tutorial

From the course: Java Design Patterns: Creational

Start my 1-month free trial

Avoid complex constructors

- [Instructor] The main purpose of the builder pattern is to avoid having complex constructors. In general, you should avoid having a constructor that takes a large number of parameters. Long lists of parameters are confusing and do not give you much flexibility when creating a class. Consider the case of a class representing a person. Each new person created will have a unique combination of characteristics. For example, each person will have a name, age, hair color, eye color, occupation, nationality, and so on and so forth. In a Java application, every time a person was created, all of this information would have to be passed into the constructor. This is a very long list that needs to be supplied, and it is easy to see how items might be put in the wrong order. There might also be times in the application where not all of the information is needed. You might need to create a person but only want to specify their name. To create a person you would still have to supply all of the…

Contents