Kotlin Builder Pattern Simplified

Aug 05, 2020


Let’s examine the solution of the builder pattern, one of the creational design patterns and how Kotlin simplifies its creation.

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns. Item number 2 in Effective Java describes how this creational pattern can be used effectively in Java. Quoting Joshua Bloch:

“Use BUILDERS when faced with many constructors”

Let’s not see an example of me adding multiple constructors just to prove a point and skip to the nice code:

public class Programmer {
    private final String name;
    private final int age;
    private final int favoriteNumber;
    private final String favoriteColor;
    private final boolean dislikesJS;
}
public static class Builder {
    // Required parameters
    private final String name;
    private final int age;
    // Optional parameters - initialized with default values
    private int favoriteNumber = 0;
    private String favoriteColor = "";
    private boolean dislikesJS = true;

       public Builder(String name, int age) {
           this.name \= name;
           this.age \= age;
       }

       public Builder favoriteNumber(int val) {
           favoriteNumber \= val;
           return this;
       }

       public Builder favoriteColor(String val) {
           favoriteColor \= val;
           return this;
       }

       public Programmer build() {
           return new Programmer(this);
       }
}
private Programmer(Builder builder) {
    name = builder.name;
    age = builder.age;
    favoriteNumber = builder.favoriteNumber;
    favoriteColor = builder.favoriteColor;
    dislikesJS = builder.dislikesJS;
}

This is how we instantiate it:

final Programmer programmer =
    new Builder("John", 30)
    .favoriteNumber(1729)
    .favoriteColor("Blue")
    .build();

Looks right and most importantly nice and neat. But…

With Kotlin you don’t need to use a builder pattern at all because there is the feature of default parameters, which allow you to define default values for each optional constructor argument.

But what about the Builder pattern feature of simulating named optional params as in Python you say…

Hm… first of all, why are you interrupting and who are you? As a matter of fact, I was about to get there… ლ(ಠ_ಠ ლ)

Well, in Kotlin we don’t need to simulate named optional parameters because we can use them directly.

class Programmer(
    private val name: String,
    private val age: Int,
    private val favoriteNumber: Int = 0,
    private val favoriteColor: String = "",
    private val dislikesJS: Boolean = true
)

Creating an object in Kotlin then looks like this:

val programmer = Programmer(Name = "John", Age = 30, favoriteNumber = 1729, favoriteColor = "Blue")

For better readability, you can also name the required parameters.

Like Java, the object created here is immutable. We reduced the lines of code needed from 2871 in Java to 2 in Kotlin, resulting in a huge productivity boost.

Note* Default params are something unknown to Java and if we want to write Java-friendly code, we can simply add @JvmOverloads to the constructor and use one of the greatest selling points of Kotlin, 100% interoperability with Java.

class Programmer @JvmOverloads constructor(
    private val name: String,
    private val age: Int,
    private val favoriteNumber: Int = 0,
    private val favoriteColor: String = "",
    private val dislikesJS: Boolean = true
)

Tanja Zlatanovska

Book a Free Consultation

Select your industry*

Please select your industry*

Select your service type

Please select your service type

When is the best time to get in touch with you

The fields marked with * are required

AI in smart manufacturing cover

AI in Smart Manufacturing: A Practical Roadmap to Data-Driven, Resilient Production

Mar 04, 2025
Imagine stepping onto a factory floor where machines anticipate their own maintenance, AI systems catch product defects invisible to the human eye, and supply chain hiccups solve themselves before you even notice a delay. This isn’t a distant dream - it’s the new baseline for competitive manufacturing operations. Welcome to the era of AI in smart manufacturing, where artificial intelligence, mach
Monika Gjorgjievska

Monika Gjorgjievska

The-Different-Types-of-Vector-Stores-and-Their-Role-in-Data-Retrieval

The Different Types of Vector Stores and Their Role in Data Retrieval

Feb 17, 2025
In the current era of technological advancements, it is evident that AI has revolutionized the operations of industrial tasks as well as day-to-day activities. One of the significant means of data retrieval for AI and machine learning applications is vector stores to which specifically, Bittensore vector store has made an extraordinary entrance to the technological field. Acting as a cutting-edge
Stefan Gligorov

Stefan Gligorov

How-Conversational-AI-Business-Intelligence-is-Revolutionizing?

How Conversational AI Business Intelligence is Revolutionizing?

Feb 10, 2025
Nowadays, the global business landscape has inevitably been influenced by conversational Artificial Intelligence (AI) to which we have to learn how to keep up with its pace. The accessibility of conversational AI Business Intelligence has demonstrated its capability to provide the luxury of [real-time insights](https://www.aimtechnologies.co/real-time-insights-the-game-changer-for-modern-businesse
Stefan Gligorov

Stefan Gligorov

View all posts