Builder

This lesson discusses the Builder pattern which is extensively used in Java.

We'll cover the following...

Question # 1

What is the Builder pattern?

Builder is one of the most common creational patterns out there. Usually, when a class evolves it adds parameters to its existing constructors and creates a whole slew of new constructors. Eventually, a class will end up with a number of very similar constructors that differ in the number of their parameters.

class SomeClass {

    // telescoping constructors

    SomeClass() { }

    SomeClass(param1) { }

    SomeClass(param1, param2) { }

    SomeClass(param1, param2, param3) { }

    SomeClass(param1, param2, param3, param4) { }

}

The list of constructors starts to resemble a telescope and the pattern is called telescoping constructors. The pattern reduces readability and increases the chance of error by the consumers of the class.

The builder pattern's idea is instead of making the desired object directly, the client calls a constructor (or static factory) with all of the required parameters and gets a ...