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
| 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
| 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 { | |
| 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
| 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
| /* 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() |
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 XmlFileParser : FileParser | |
| class JsonFileParser : FileParser |
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 |
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
| { | |
| "time": "9:30pm" | |
| } |
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
| { | |
| "client_id": "my-id", | |
| "date": "2018–12–12", | |
| "time": "9:00pm", | |
| "number_of_guests": 2 | |
| } |