Kotlin Builder Pattern Simplified.png

Kotlin Builder Pattern Simplified

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

Aug 05, 2020

Category

Article

Technologies

JavaKotlin

Let's talk.

Name
Email
Phone
By submitting this, you agree to our Terms and Conditions & Privacy Policy