Created
April 4, 2020 16:50
-
-
Save jimfilippou/026091c30dfcc480b84de756ec5724f7 to your computer and use it in GitHub Desktop.
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
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