Skip to content

Instantly share code, notes, and snippets.

@sliskiCode
Created October 21, 2016 13:01
Show Gist options
  • Save sliskiCode/f2babddb0bf3c42a4a49b87831560ac8 to your computer and use it in GitHub Desktop.
Save sliskiCode/f2babddb0bf3c42a4a49b87831560ac8 to your computer and use it in GitHub Desktop.
Builders in Kotlin. Gist 2
class Person(val name: String, val surname: String, val age: Int) {
companion object {
class Builder {
private var name: String = ""
private var surname: String = ""
private var age: Int = 0
fun name(name: String): Builder {
this.name = name
return this
}
fun surname(surname: String): Builder {
this.surname = surname
return this
}
fun age(age: Int): Builder {
this.age = age
return this
}
fun build() = Person(name, surname, age)
}
}
}
fun main(args: Array<String>) {
Person.Companion
.Builder()
.name("Piotr")
.surname("Slesarew")
.age(28)
.build()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment