-
-
Save FredDeschenes/cb5a3c822f8b1396d0d291773977567c to your computer and use it in GitHub Desktop.
Kotlin builder demo
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 PublicClassProtectedConstructor private constructor(val foo: Int) { | |
fun printFoo() { | |
println(foo) | |
} | |
class Builder private constructor() { | |
var foo: Int = 0 | |
private fun build(): PublicClassProtectedConstructor { | |
return PublicClassProtectedConstructor(foo) | |
} | |
companion object { | |
operator fun invoke(block: Builder.() -> Unit): PublicClassProtectedConstructor { | |
return Builder().apply(block).build() | |
} | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
val theThing = PublicClassProtectedConstructor.Builder { | |
foo = 123 | |
} | |
theThing.printFoo() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment