Implementing the builder pattern

The Builder pattern is a creational pattern whose goal is to simplify object construction. Instead of having complex constructors that take a large number of parameters, this pattern provides ways to create objects with different states of representation. It gives flexibility while instantiating a class. It provides builder functions to construct the object step by step, and the object will be returned in the final step.

Consider a class that represents a person. We will have to create person objects with different combinations of characteristics. Say, for example, person with firstName, lastName, age, contactNumber, loginId, and address. The class looks as follows:

class Person {    var firstName: String? = ...

Get Kotlin for Enterprise Applications using Java EE now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.