Skip to content

Instantly share code, notes, and snippets.

@markmaynard
Last active June 1, 2019 17:49
Show Gist options
  • Save markmaynard/fe2da8f4200282681ab6b5e7ecfa1f09 to your computer and use it in GitHub Desktop.
Save markmaynard/fe2da8f4200282681ab6b5e7ecfa1f09 to your computer and use it in GitHub Desktop.
ThingFactory attempt number 1
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