-
-
Save 5AbhishekSaxena/8770cedd6e1ddfaaa9f6ec840af9cb01 to your computer and use it in GitHub Desktop.
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 User private constructor( | |
val firstName: String, | |
val middleName: String?, // optional | |
val lastName: String, | |
val address: Address, | |
val contact: Contact?, // optional | |
val company: Company?, // optional | |
val educations: List<Education>, | |
) { | |
class Builder { | |
var firstName: String? = null | |
var middleName: String? = null // optional | |
var lastName: String? = null | |
var address: Address? = null | |
var contact: Contact? = null // optional | |
var company: Company? = null // optional | |
var educations: List<Education>? = null | |
fun setFirstName(firstName: String): Builder { | |
this.firstName = firstName | |
return this | |
} | |
// OR | |
fun setMiddleName(middleName: String) = apply { | |
this.middleName = middleName | |
} | |
fun setLastName(lastName: String) = apply { | |
this.lastName = lastName | |
} | |
fun setAddress(address: Address) = apply { | |
this.address = address | |
} | |
fun setContact(contact: Contact) = apply { | |
this.contact = contact | |
} | |
fun setCompany(company: Company) = apply { | |
this.company = company | |
} | |
fun setEducations(educations: List<Education>) = apply { | |
this.educations = educations | |
} | |
fun build(): User { | |
return User( | |
firstName!!, | |
middleName, | |
lastName!!, | |
address!!, | |
contact, | |
company, | |
educations!! | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment