Created
January 19, 2020 20:00
-
-
Save balsikandar/39c08eb198ed7c6c5972e0d8f7a5e6b2 to your computer and use it in GitHub Desktop.
Student Builder pattern example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student( | |
val name: String?, | |
val standard: Int, | |
val rollNumber: Int | |
) { | |
private constructor(builder: Builder) : this(builder.name, builder.standard, builder.rollNumber) | |
class Builder { | |
var name: String? = null | |
private set | |
var standard: Int = 0 | |
private set | |
var rollNumber: Int = 0 | |
private set | |
fun name(name: String) = apply { this.name = name } | |
fun standard(standard: Int) = apply { this.standard = standard } | |
fun rollNumber(rollno: Int) = apply { this.rollNumber = rollNumber } | |
fun build() = Student(this) | |
} | |
} | |
//To create Student object now we can use | |
Student.Builder() | |
.name("Alex") | |
.standard(10) | |
.rollNumber(720) | |
.build() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment