A quick guide into Builder Design Pattern in Android


This quick guide to the Builder Design Pattern is perfect whether you're looking for a quick revision or aiming to grasp the concept from scratch
The Builder Pattern is a creational design pattern used to construct complex objects step by step. It’s especially useful when an object has many optional fields or when you want to make object creation more readable and manageable.
If a class has many parameters, instead of adding all of them to the constructor and creating a telescoping constructor, the Builder Pattern provides a better approach by allowing you to add or separate optional parameters
Let’s understand with a clear example using the Person
class. In this class, there are fields like firstName
, lastName
, middleName
, age
, phoneNo
, and address
. Among these, firstName
, lastName
, and age
are the only mandatory fields.
So without builder pattern it will looks something like this :
class Person(val firstName: String, val lastName: String, val age: Int) {
// Telescoping constructor for different combinations of parameters
constructor(firstName: String, lastName: String, age: Int, middleName: String) : this(firstName, lastName, age)
constructor(firstName: String, lastName: String, age: Int, middleName: String, phoneNo: String) : this(firstName, lastName, age, middleName)
constructor(firstName: String, lastName: String, age: Int, middleName: String, phoneNo: String, address: String) : this(firstName, lastName, age, middleName, phoneNo)
}
fun main() {
val person1 = Person("John", "Doe", 30) // Only mandatory fields
val person2 = Person("Alice", "Smith", 25, "Marie") // Mandatory + middle name
val person3 = Person("Bob", "Johnson", 40, "David", "123-456-7890") // Mandatory + middle name + phone number
val person4 = Person("Charlie", "Brown", 35, "Robert", "987-654-3210", "1234 Elm St") // All fields
}
A better approach to build this class using Builder Pattern :
fun main() {
val p1 = Person.PersonBuilder(name = "Shalen", lastName = "mathew", age = 22)
.phoneNo(911)
.address("Banglore")
.build()
}
data class Person(
val name: String?,
val lastName: String?,
val age: Int?,
val middleName: String?=null,
val phoneNo: Int?=null,
val address: String?=null
){
class PersonBuilder(val name: String,val lastName: String,val age: Int){
var middleName: String? = null
var phoneNo:Int? = null
var address:String?=null
fun middleName(middleName: String?) = apply {
this.middleName = middleName
}
fun phoneNo(phoneNo: Int?) = apply {
this.phoneNo = phoneNo
}
fun address(address: String?) = apply {
this.address = address
}
fun build(): Person {
return Person(name, lastName, age, middleName, phoneNo, address)
}
}
}
Lets understand what’s going on in the code
The Person
class has fields like name
, lastName
, age
, middleName
, phoneNo
, and address
. Some of these fields are optional (like middleName
, phoneNo
, and address
).
Inside the Person
class, there's a PersonBuilder
class. This class helps you build a Person
object step-by-step. The mandatory field are defined in the constructor of PersonBuilder
class
How It Works:
You create a
PersonBuilder
by passing the required fields (name
,lastName
,age
).Then, you can optionally add the optional fields (
middleName
,phoneNo
,address
) using setter methods like.middleName()
,.phoneNo()
, and.address()
.Each setter method uses
apply
to set the value and return the builder itself. This allows chaining multiple calls.Finally, you call
.build()
to create thePerson
object with all the values.
Kotlin give a better optimized way to recreate this…while the above code follows a more Java-style implementation
In Kotlin, we can use a data class to achieve a builder-like effect by defining default values for parameters, making them optional. This eliminates the need for a separate builder class while maintaining flexibility in object creation.
data class Person(
val name: String,
val lastName: String,
val age: Int,
val middleName: String? = null,
val phoneNo: Int? = null,
val address: String? = null
)
fun main() {
// Using named arguments for the constructor
val person = Person(
name = "Shalen",
lastName = "Mathew",
age = 22,
phoneNo = 911,
address = "Bangalore"
)
println(person)
}
Explanation:
The
data class
Person
has both required (name
,lastName
,age
) and optional (middleName
,phoneNo
,address
) parameters.The optional parameters have already being defined with default values (
null
in this case) , so you can skip them when creating aPerson
object.
That’s all from me for today 😁. If I am lacking somewhere or this article needs some correction just comment down. Byeeee!!!👋
Follow me on socials :
X , LinkedIn , Github, Butterfly , Linktree
Subscribe to my newsletter
Read articles from Shalen Mathew directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shalen Mathew
Shalen Mathew
I write about Android dev & Open Source