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