Builder Pattern + Telescopic Constructor Let’s Understand it Part 1

AnyOneCanCode
3 min readSep 16, 2023

--

Builder pattern is one of a unique pattern that allows us to delegate object creation responsibility into separate dedicated object which allows us to easily create different representational state of same object.

So builder pattern is basically used -
1. Delegating complex object creation responsibility to builder you can also say encapsulating object creation
2. Managing different representational state of same object

Let’s understand the problem first, suppose there is a some class having A,B,C attribute

// Any class doesn't matter
Class RandomName {
A, // these are attributes A, B, C
B,
C
}

While object creation we need to support different state initialisation

// Any class doesn't matter
Class RandomName {
A, // these are attributes A, B, C
B,
C
// defining all possible constructor
RandomName() {}
RandomName(A) {this.A = A}
RandomName(B) {this.B = B}
RandomName(C) {this.C = C}
RandomName(A,B) {this.A = A; this.B = B;}
RandomName(B,C) {this.B = B; this.C = C;}
......// all possible state representations
}

This constructor overloading supporting all possible attributes is known as telescopic construction.

Now this is overhead as code looks very ugly & supporting all possible parameter construction is not viable.

Delegating complex object creation all over the code base can lead to mess.

Different representation state of object is spreaded across everywhere

Let’s delegate house creation to house builder. builder pattern allows us to create object steps by steps & didn’t create final object until it’s completed. it also allows use to make attribute editable even when object field is defined as final.

Delegating house create responsibility to builder

Similarly when using document builder we can build document steps by steps & keeps on changing until calling result.

DocumentBuilder builder = new DocumentBuilder()
builder.title("one title") // not create actual building object
builder.title("title change"). // building object steps by steps
builder.text("my name is naman")
builder.name(my document")

Document document = builder.result() // now object is finally created

We can also create many concrete builder implementation of object. let’s say we want different builder of luxury, cheap & normal house.

Different concrete builder of object

Note: Don’t get confuse between builder & factory method as builder only responsibility it to build object not diving into polymorphism

This part we tries to understand builder pattern theoretically Part 2 we will be focusing on coding example.

Part 2
https://any-one-can-code.medium.com/builder-pattern-coding-explanation-part-2-469e3e000723

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response