Created
June 8, 2019 12:22
-
-
Save markmaynard/defe0afe7292866c965615e9f0392d8f to your computer and use it in GitHub Desktop.
Part 1 code complete
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