- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to implement Builder pattern in Kotlin?
In object-oriented programming, the creation of objects is very easy via constructors when the object definitions are simple, but sometimes, it may so happen that the constructors need more variables or functions in order to initialize an object. In such cases, the "builder pattern" comes into picture which helps programmers to create small units of different objects and create a final object.
A builder pattern provides an API to construct an object step- by-step.
Builder patterns are particularly useful when objects need to be created dynamically.
Note that it is not recommended to use builder patterns in Kotlin because we can get the same feature using default and named arguments.
In this article, we will take a simple example and demonstrate how we can implement a design pattern using Kotlin.
Create a small builder object, which can be built in the main class and the builder class should have a public constructor .
Construct an object using multiple pieces; some variables can be set using getter, setter and some via constructor.
The builder design pattern provides an API to create an object. We are creating build() to build the actual required object. We can name it differently whatever we need.
The main class constructor will take the builder object as a parameter.
Example
class myBuilder{ private var Name:String? = null fun setName(Name:String){ this.Name = Name } fun getName():String?{ return this.Name } } class myClass{ var m:myBuilder constructor(myBuilder:myBuilder) { this.m = myBuilder } // implementing the builder class fun build():String{ return ("This is the example of the builder pattern") } } fun main(args: Array<String>) { var myBuilder = myBuilder() myBuilder.setName("TutorialsPoint") // passing small unit of objects to create // another object step-by-step. This can be // applied to many class variable var mainObj = myClass(myBuilder) println(mainObj.build()) }
Output
Once executed, the code will generate the following output
This is the example of the builder pattern
- Related Articles
- How to implement Null object Pattern in C#?
- How to implement IDisposable Design Pattern in C#?
- How to implement AlarmManager in Android using Kotlin?
- How to implement switch-case statement in Kotlin?
- How to implement a Singleton design pattern in C#?
- How to use string Builder class in android?
- What is proxy design pattern and how to implement it in C#?
- How to implement an endless list with RecyclerView in Android using Kotlin?
- How to extend and implement at the same time in Kotlin?\n
- How to implement a long click listener on an Android listview using Kotlin?
- LongStream builder() method in Java
- DoubleStream builder() method in Java
- IntStream builder() method in Java
- How to alias a table in Laravel Eloquent queries using Query Builder?
- MediaPlayer class to implement a basic Audio Player in an Android Kotlin app?
