Skip to content

Instantly share code, notes, and snippets.

@markmaynard
Created June 8, 2019 12:22
Show Gist options
  • Save markmaynard/defe0afe7292866c965615e9f0392d8f to your computer and use it in GitHub Desktop.
Save markmaynard/defe0afe7292866c965615e9f0392d8f to your computer and use it in GitHub Desktop.
Part 1 code complete
class ThingFactory {
inline fun <reified T: Any> run() {
if (myThing == null) {
createThing<T>()
}
myThing!!.run()
}
inline fun <reified T: Any> play() {
if (myThing == null) {
createThing<T>()
}
myThing!!.play()
}
inline fun <reified T: Any> createThing() {
when(T::class) {
Thing1::class -> myThing = Thing1()
Thing2::class -> myThing = Thing2()
else -> throw IllegalArgumentException("Unknown thing")
}
}
companion object {
var myThing: SomeThing? = null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment