Created
October 21, 2016 13:01
-
-
Save sliskiCode/f2babddb0bf3c42a4a49b87831560ac8 to your computer and use it in GitHub Desktop.
Builders in Kotlin. Gist 2
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 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