Skip to content

Instantly share code, notes, and snippets.

@jimfilippou
Created April 4, 2020 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimfilippou/026091c30dfcc480b84de756ec5724f7 to your computer and use it in GitHub Desktop.
Save jimfilippou/026091c30dfcc480b84de756ec5724f7 to your computer and use it in GitHub Desktop.
interface Virus {
public fun mutate()
public fun spread() {
println("Spreading the virus...")
}
}
class CoronaVirus: Virus {
override fun mutate() {
println("Mutating the corona virus...")
}
}
class InfluenzaVirus: Virus {
override fun mutate() {
println("Mutating the flu virus...")
}
}
class HIVVirus: Virus {
override fun mutate() {
println("Mutating the HIV virus...")
}
}
class VirusFactory {
fun makeVirus(type: VirusType): Virus? {
return when(type) {
VirusType.CORONA_VIRUS -> CoronaVirus()
VirusType.INFLUENZA -> InfluenzaVirus()
VirusType.HIV -> HIVVirus()
else -> null
}
}
}
enum class VirusType {
CORONA_VIRUS,
INFLUENZA,
HIV
}
fun main() {
val factory = VirusFactory()
val virus = factory.makeVirus(VirusType.CORONA_VIRUS)
virus?.spread()
virus?.mutate()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment