Skip to content

Instantly share code, notes, and snippets.

@Adnan9011
Created May 23, 2022 02:30
Show Gist options
  • Save Adnan9011/b2857d81c3b896ce0b3f9dce0e1dbd8f to your computer and use it in GitHub Desktop.
Save Adnan9011/b2857d81c3b896ce0b3f9dce0e1dbd8f to your computer and use it in GitHub Desktop.
class Person(
val name:String,
val family:String,
val age:Int,
val nationalCode: String?,
val email: String?,
val phoneNumber: String?
) {
// 1 - Private constructor
private constructor(builder: Builder) : this (
builder.name,
builder.family,
builder.age,
builder.nationalCode,
builder.email,
builder.phoneNumber
)
// 2 - Builder class
// 3 - Necessary parameters in Builder class : name , family
class Builder(val name :String,val family :String) {
// 4 - Optional parameters in Builder class :
var age: Int = 0
private set
var nationalCode: String? = null
private set
var email: String? = null
private set
var phoneNumber: String? = null
private set
fun age(age: Int) = apply { this.age = age }
fun nationalCode(nationalCode: String) =
apply { this.nationalCode = nationalCode }
fun email(email: String) = apply { this.email = email }
fun phoneNumber(phoneNumber: String) =
apply { this.phoneNumber = phoneNumber }
// 5 - Create
fun create() = Person(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment