Effective Java — Item 2 — II

Kasumi Gunasekara
3 min readNov 23, 2020

--

Consider a builder when faced with many constructor parameters

Photo by Sarah Pflug from Burst

Recently, I have been reading Effective Java by Joshua Bloch which provides a definitive guide to best practices of Java Language. The book has been arranged with “Items” throughout the chapters with a common numbering pattern. This story is about the second part of Item 2 which comes under Chapter 2: Creating and Destroying Objects. You can find Effective Java — Item 2 — I here.

Consider a builder when faced with many constructor parameters

There can be situations where the constructor has multiple parameters. In previous story regarding Item 2 of Effective Java, the approaches that the programmers have followed in such scenarios are discussed. In this story, we will elaborate on the suitability of Builder pattern for class hierarchies.

Builder pattern is well suited for class hierarchies

Let’s consider an abstract Pizza class which represents a hierarchy for various kinds of Pizza.

NewYorkStylePizza lets to provide the size of the Pizza. The build() method of NewYorkStylePizza.Builder returns NewYorkStylePizza.

CalzonePizza lets to choose whether the sauce should be inside or out. The build() method of CalzonePizza.Builder returns CalzonePizza.

Since the Builders of sub classes return their own type, there is no need for a casting from super class type to a sub type.

Calzone calzone = new Calzone.Builder()
.addTopping(HAM)
.sauceInside()
.build();

This technique, where in a subclass method is declared to return a sub type of the return type declared in the super class, is known as covariant return typing.

The Builder pattern approach is flexible for creating an instance. The parameters of the builder can be introduced after creating the object, and before invoking the build() method.

A builder can fill in some fields automatically upon object creation, such as a serial number that increases each time an object is created.

The usage of builders than telescoping constructors will improve the readability and maintainability of the code, and builders are much safer than using JavaBeans pattern.

Cons….

  • Even though the Builder Pattern has quite the advantages when creating object flexibly, the cost of creating a builder for a class can be an issue in a performance critical scenarios.
  • The Builder pattern is more descriptive than Telescoping pattern. Therefore, if Builder pattern is chosen, it should be more parameters that would be worth to have Builder pattern rather than Telescoping pattern.
  • Furthermore, if it tends to grow the number of parameters with future changes, it is appropriate to use Builder pattern in the first place rather than using static factory method or set of constructors.

In conclusion…..

Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

Photo by Masjid Pogung Dalangan on Unsplash

I hope this article has been useful for you. If I missed anything, please let me know.

Stay Safe!!!

--

--