Introduction to Kotlin Builder Pattern
Kotlin builder pattern is used to handle object building which contains lots of parameters, and at the time of making the mutable object, then we are constructing it, which was often used but rarely creating our own. Kotlin is providing features that were useful such as default and named parameters data class and apply which is avoiding the use of builder pattern.
By using the well-named function as a builder in a combination with the literal function which was receiving type safe and statistically typed builders in a kotlin. The builder pattern in a kotlin is a famous design pattern that we are using for the creation of objects. The builder pattern in kotlin will allow us to create an immutable object which was complex in a step-by-step manner. This pattern will be offering the opportunity to attach the different properties to the object by using the chaining procedure of the fluent method. It is very useful and important.
Key Takeaways
- The default parameter of kotlin is relieving us from overloading multiple constructors, but still, the constructor contains the optional parameter, which was numerous.
- In the builder pattern, we are creating the small object for creating the final object; we are constructing an object which is providing the API.
How to Create a Kotlin Builder Pattern?
For creating the builder pattern in kotlin first, we need small builder objects to create a final object of our main class and build the same in the main class. The builder class contains the constructor, which was public and it will contain all the required parameters. Then we construct an object into the prices, but we need to sure that end result will same. We need to create getter and setter as a variable is private, so anyone cannot access the same from the outside. Also, we are providing access by using getter and setter methods.
The design pattern of the builder provides an API for constructing the object step by step. In the main class, we have implemented the function as build, and we can name the same as get or also we can give any name to that function for making the target object. Main class constructor is accepting the builder object as a parameter.
In the below example, we are creating a small builder object which was built in the builder class and main class and which contains the public constructor. Also, we are constructing multiple pieces for the same variable. The design pattern of the builder provides API for creating the object.
Code:
class kotlin private constructor(
val property: Any,
val optionalProperty: Any?
) {
class imp (private val requiredProperty: Any) {
private var optionalProperty: Any? = null
fun optionalProperty(value: Any?): imp {
this.optionalProperty = value
return this
}
fun build(): kotlin {
return kotlin (requiredProperty, optionalProperty)
} }
}
Output:
Kotlin Builder Pattern – Implement Builder
In programming of object-oriented by using constructor creation of an object is very easy at the time object definition will be simple. Still, sometimes the constructor will need more functions and variables to initialize the object. In the same scenario, we are using a builder pattern, it will create a small unit of the object which was different and also create the final object.
Builder pattern in kotlin provides an API to construct the object step by step. Builder pattern is particularly useful when we need to create the objects dynamically. But it is not recommended to use the builder pattern in kotlin because we are getting the same feature by using named and the default arguments.
Below steps show how we can implement the builder pattern as follows:
- Create the small builder object.
- Construct the object also set the variable by using getter and setter method.
- Create a build method for building the required object.
- The main class constructor takes the parameter of the builder object.
The below example shows how we can implement the builder pattern in kotlin as follows. We are creating the class name as kot_builder.
Code:
class kot_builder
{
private var stud_name:String? = null
fun setName (stud_name:String) {
this.stud_name = stud_name
}
fun getName ():String?{
return this.stud_name
}
}
class builder{
var m:kot_builder
constructor (kot_builder:kot_builder) {
this.m = kot_builder
}
fun build():String{
return ("Kotlin builder pattern")
}
}
fun main(args: Array<String>)
{
var kot_builder = kot_builder()
kot_builder.setName ("Kotlin Builder")
var obj = builder(kot_builder)
println (obj.build())
}
Output:
Examples
Given below are the examples mentioned:
Example #1
In kotlin some object is simple, and it was created by using a constructor, which was single.
Code:
class builder_pattern
{
private var emp_add:String? = null
fun setName (emp_add:String) {
this.emp_add = emp_add
}
fun getName ():String?{
return this.emp_add
}
}
class kot {
var m:builder_pattern
constructor (builder_pattern:builder_pattern) {
this.m = builder_pattern
}
fun build ():String{
return ("Example Kotlin builder pattern")
}
}
fun main(args: Array<String>)
{
var builder_pattern = builder_pattern()
builder_pattern.setName ("Builder Pattern")
var obj = kot (builder_pattern)
println (obj.build())
}
Output:
Example #2
In the below example, we are using a constructor in a nested build class to access the class as follows.
Code:
class kotlin_builder {
var stud_name: String? = null
private set
var stud_addr: String? = null
private set
fun stud_name(stud_name: String) = apply { this.stud_name = stud_name }
fun stud_addr(stud_addr: String) = apply { this.stud_addr = stud_addr }
fun build() = student(this)
}
Output:
FAQ
Given below is the FAQ mentioned:
Q1. How do we declare it?
Answer:
The below example shows how we can declare the builder pattern in our code as follows.
Code:
val stud = stud.Builder ()
.firstName ("ABC")
.lastName ("PQR")
.build()
Q2. What is used of builder pattern in kotlin?
Answer:
It is used to create the object. By using builder pattern we can create the immutable object in kotlin.
Q3. What is the purpose of it?
Answer:
The main purpose of the builder pattern is to create an object which was complex. By using it, we are setting builder object properties. Using the builder pattern, we can pass many arguments because every builder method takes only one.
Conclusion
The builder pattern in a kotlin is a famous design pattern that we use to create objects. It is used to handle object building which contains lots of parameters, and at the time of making the mutable object, we are constructing it.
Recommended Articles
This is a guide to Kotlin Builder Pattern. Here we discuss the introduction and how to create it. Implement builder, examples. You may also have a look at the following articles to learn more –