This file contains hidden or 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
| val parserFactory = StandardFileParserFactory() | |
| val fileParser = parserFactory.createFromFileName(“filename.xml”) |
This file contains hidden or 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 CompanionObjectFileParserFactory { | |
| companion object : 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.") | |
| } | |
| } | |
| } |
This file contains hidden or 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
| MyFileParserFactory.createFromFileName("filename.json") |
This file contains hidden or 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 CompanionObjectFileParserFactory : FileParserFactory { | |
| companion object : 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.") | |
| } | |
| } | |
| /* needed if you want to be able to call the createFromFileName from an instance */ |
This file contains hidden or 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
| object ObjectFileParserFactory : 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.") | |
| } | |
| } |
This file contains hidden or 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
| //Works… | |
| ObjectFileParserFactory.createFromFileName("filename.foo") | |
| //...and this still works | |
| val fileParser = ObjectFileParserFactory | |
| fileParser.createFromFileName("filename.json") |
This file contains hidden or 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 FileParser { | |
| companion object { | |
| fun createFromFileName(fileName: String) = | |
| when (fileName.substringAfterLast('.')) { | |
| "xml" -> XmlFileParser() | |
| "json" -> JsonFileParser() | |
| else -> throw Exception("I don't know how to deal with $fileName.") | |
| } | |
| } | |
| } |
This file contains hidden or 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
| FileParser.createFromFile("filename.json") |
This file contains hidden or 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
| fun FileParser.Companion.fromFile(filename: String) = | |
| this.createFromFileName(filename) |
This file contains hidden or 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
| val parser = FileParser fromFile "filename.json" |