liveBook · Manning

Telescoping constructor

As explained in The Art of Code

A telescoping constructor is a construction pattern that provides multiple overloaded constructors for different subsets or combinations of optional parameters.

A telescoping constructor handles optional data by adding overloaded constructors for different parameter combinations. It starts clearly enough with a few variants, but becomes unreadable and difficult to maintain as fields accumulate. section 4.1.2

Why it becomes fragile

A Person object with ten fields can require arguments such as null, null, 38000, "France", and several integers. The call hides what each value means and depends on exact parameter order. Supporting every combination could require up to 1,024 constructors, while some combinations cannot coexist or share a signature. section 4.1.2

public Person(String firstname) { ... }
public Person(String firstname, String lastname) { ... }
public Person(String firstname, String lastname, String town) { ... }

Compare progressively longer overloaded Person constructors and see how the pattern expands. section 4.1.2

The Builder pattern is the practical alternative: callers name only the values they need, producing more readable and flexible construction. In Java, Lombok can generate builder support with @Builder, reducing manual boilerplate. section 4.1.2

How The Art of Code covers it

How deep it goes
Defines the pattern, demonstrates its scaling problem, and stops short of detailed implementation guidance.
Examples
Uses ten-field and two-field Person examples, overloaded constructors, null arguments, and a fluent builder call.
What it assumes
Assumes object-oriented constructors and optional parameters while grounding examples in Java.
What you can do after
Evaluate object-construction APIs and choose a Builder pattern when overloaded constructors become excessive.
How it connects
Connects overloaded constructors with encapsulation, data modeling, and the Builder pattern.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.