Created
December 9, 2017 16:48
-
-
Save geraldcroes/111cb13f7a9d46ae02c1fd086566a6a6 to your computer and use it in GitHub Desktop.
StandardFileParserFactory
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
/* The concept of the factory -> creates a Product */ | |
interface FileParserFactory { | |
fun createFromFileName(fileName: String): FileParser | |
} | |
/* Our specific Factory */ | |
class StandardFileParserFactory : FileParserFactory { | |
override fun createFromFileName(fileName: String) = | |
when (fileName.substringAfterLast('.')) { | |
"xml" -> XmlFileParser() | |
"json" -> JsonFileParser() | |
else -> throw Exception("I don't know how to deal with $fileName.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment