Skip to content

Instantly share code, notes, and snippets.

@geraldcroes
Created December 9, 2017 16:48
Show Gist options
  • Save geraldcroes/111cb13f7a9d46ae02c1fd086566a6a6 to your computer and use it in GitHub Desktop.
Save geraldcroes/111cb13f7a9d46ae02c1fd086566a6a6 to your computer and use it in GitHub Desktop.
StandardFileParserFactory
/* 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