Course – LS – NPI (cat=Java)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING

1. Overview

In this tutorial, we’ll learn about the challenges in implementing the Builder Design Pattern while dealing with hierarchal inheritance. An example of a hierarchical inheritance could be the inheritance between an electric car, a car, and a vehicle.

Builder Pattern is a creational design pattern that helps simplify building complex objects having many attributes in a step-by-step process with the help of method chaining. While inheritance helps simplify design, it also leads to complexity in implementing method chaining to create objects in the Builder Pattern.

Further, we’ll come up with an efficient implementation with the help of Java Generics API.

2. Problem Description

Let’s take an example of applying the Builder Pattern in creating objects of type Vehicle, Car and ElectricCar:

 

At the top of the object hierarchy, there is the Vehicle class. The class Car extends the Vehicle and then the ElectricCar extends Car. Similar to these objects their builders also have a hierarchical relationship between them.

Let’s instantiate the CarBuilder class, set its attributes with method chaining, and finally call the build() method to get the car object:

CarBuilder carBuilder = new CarBuilder();
Car car = carBuilder.make("Ford")
  .model("F")
  .fuelType("Petrol")
  .colour("red")
  .build();

Let’s try to change the order of the method calls:

CarBuilder carBuilder = new CarBuilder();
Car car = carBuilder.make("Ford")
  .colour("red")
  .fuelType("Petrol")
  .model("F")
  .build();

The methods colour() and fuelType() return VehicleBuilder class. Hence, the subsequent call to model() would cause a compilation error because it’s absent in the VehicleBuilder class. This is inconvenient and a drawback. Similar behaviour is seen when we try to build the ElectricVehicle object with the ElectricVehicleBuilder class.

3. Solution Without Generics

This is a very straightforward implementation where the child builder classes override the chaining methods of all the base builder classes in the hierarchy. Hence, there is no compilation error during method chaining to set the attribute values.

Let’s understand this by first taking a look at the Vehicle class:

public class Vehicle {

    private String fuelType;
    private String colour;

    // Standard Getter methods..
    public Vehicle(VehicleBuilder builder) {
        this.colour = builder.colour;
        this.fuelType = builder.fuelType;
    }

    public static class VehicleBuilder {

        protected String fuelType;
        protected String colour;

        public VehicleBuilder fuelType(String fuelType) {
            this.fuelType = fuelType;
            return this;
        }

        public VehicleBuilder colour(String colour) {
            this.colour = colour;
            return this;
        }

        public Vehicle build() {
            return new Vehicle(this);
        }
    }
}

The Vehicle class has two attributes fuelType and colour. It also has an inner class VehicleBuilder with methods with a name similar to the attributes in the Vehicle class. They return the builder class so that it can support method chaining.

Now, let’s take a look at the Car class:

public class Car extends Vehicle {

    private String make;
    private String model;

    // Standard Getter methods..

    public Car(CarBuilder builder) {
        super(builder);
        this.make = builder.make;
        this.model = builder.model;
    }

    public static class CarBuilder extends VehicleBuilder {

        protected String make;
        protected String model;

        @Override
        public CarBuilder colour(String colour) {
            this.colour = colour;
            return this;
        }

        @Override
        public CarBuilder fuelType(String fuelType) {
            this.fuelType = fuelType;
            return this;
        }

        public CarBuilder make(String make) {
            this.make = make;
            return this;
        }

        public CarBuilder model(String model) {
            this.model = model;
            return this;
        }

        public Car build() {
            return new Car(this);
        }
    }
}

The class Car has inherited from Vehicle and similarly, the CarBuilder class has inherited from VehicleBuilder. Additionally, the CarBuilder class had to override the methods colour() and fuelType().

Let’s build a Car object now:

@Test
void givenNoGenericImpl_whenBuild_thenReturnObject() {
    Car car = new Car.CarBuilder().colour("red")
      .fuelType("Petrol")
      .make("Ford")
      .model("F")
      .build();
    assertEquals("red", car.getColour());
    assertEquals("Ford", car.getMake());
}

We can set the car’s attributes in any order before calling the build() method.

However, for a child class of Car such as ElectricCar, we must override all the methods of CarBuilder and VehicleBuilder in ElectricCarBuilder. Hence, it’s not a very efficient implementation.

4. Solution With Generics

Generics can help overcome the challenges faced in the implementation discussed earlier.

For this let’s modify the inner Builder class in the Vehicle class:

public class Vehicle {

    private String colour;
    private String fuelType;

    public Vehicle(Builder builder) {
        this.colour = builder.colour;
        this.fuelType = builder.fuelType;
    }

    //Standard getter methods..
    public static class Builder<T extends Builder> {

        protected String colour;
        protected String fuelType;

        T self() {
            return (T) this;
        }

        public T colour(String colour) {
            this.colour = colour;
            return self();
        }

        public T fuelType(String fuelType) {
            this.fuelType = fuelType;
            return self();
        }

        public Vehicle build() {
            return new Vehicle(this);
        }
    }
}

Noticeably, the methods fuelType() and colour() in the inner Builder class are returning a generic type. This kind of implementation facilitates fluent style coding or method chaining. This is a design pattern well known by the name Curiously Recurring Template Pattern(CRTP).

Let’s now implement the class Car:

public class Car extends Vehicle {

    private String make;
    private String model;

    //Standard Getters..
    public Car(Builder builder) {
        super(builder);
        this.make = builder.make;
        this.model = builder.model;
    }

    public static class Builder<T extends Builder<T>> extends Vehicle.Builder<T> {

        protected String make;
        protected String model;

        public T make(String make) {
            this.make = make;
            return self();
        }

        public T model(String model) {
            this.model = model;
            return self();
        }

        @Override
        public Car build() {
            return new Car(this);
        }
    }
}

We’ve applied CRTP in the signature of the inner Builder class and made the methods in the inner class return a generic type to support method chaining.

Similarly, let’s implement the child class ElectricCar of Car:

public class ElectricCar extends Car {
    private String batteryType;

    public String getBatteryType() {
        return batteryType;
    }

    public ElectricCar(Builder builder) {
        super(builder);
        this.batteryType = builder.batteryType;
    }

    public static class Builder<T extends Builder<T>> extends Car.Builder<T> {
        protected String batteryType;

        public T batteryType(String batteryType) {
            this.batteryType = batteryType;
            return self();
        }

        @Override
        public ElectricCar build() {
            return new ElectricCar(this);
        }
    }
}

The implementation remains almost the same except for the inner Builder class extending its parent Builder.Car<T>. The same technique must be applied to the subsequent child classes of ElectricCar and so on.

Let’s see the implementation in action:

@Test
void givenGenericImpl_whenBuild_thenReturnObject() {
    Car.Builder<?> carBuilder = new Car.Builder();
    Car car = carBuilder.colour("red")
      .fuelType("Petrol")
      .make("Ford")
      .model("F")
      .build();

    ElectricCar.Builder<?> ElectricCarBuilder = new ElectricCar.Builder();
    ElectricCar eCar = ElectricCarBuilder.make("Mercedes")
      .colour("White")
      .model("G")
      .fuelType("Electric")
      .batteryType("Lithium")
      .build();

    assertEquals("red", car.getColour());
    assertEquals("Ford", car.getMake());

    assertEquals("Electric", eCar.getFuelType());
    assertEquals("Lithium", eCar.getBatteryType());
}

The method successfully builds the objects of type Car and ElectricCar.

Interestingly, we’ve used the raw generic type ? for declaring the inner classes Car.Builder<?> and ElectricCar.Builder<?>. This is because we need to ensure that method calls such as carBuilder.colour() and carBuilder.fuelType() return Car.Builder instead of its parent Vehicle.Builder.

Similarly, the method calls ElectricCarBuilder.make() and ElectricCarBuilder.model() should return ElectricCarBuilder and not CarBuilder class. Without this method chaining won’t be possible.

5. Conclusion

In this article, we discussed the challenges in the Builder Design Pattern while handling inheritance. Java Generics and Curiously Recurring Template Pattern helped us implement a solution. With this, we can use method chaining without worrying about the order of method calls to set the attribute values in the builder class.

As usual, the code used can be found over on GitHub.

Course – LS (cat=Java)

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Subscribe
Notify of
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments