Created
January 22, 2023 16:05
-
-
Save alibahaaa/5e6a5016f3f131b8d63614a139a6fa0b 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 NetworkAPI { | |
fun sendData(data: String) | |
} | |
class HTTPAPI : NetworkAPI { | |
override fun sendData(data: String) { | |
// send data over HTTP | |
} | |
} | |
class FTPAPI : NetworkAPI { | |
override fun sendData(data: String) { | |
// send data over FTP | |
} | |
} | |
abstract class DataSender { | |
protected lateinit var api: NetworkAPI | |
abstract fun send() | |
fun setAPI(api: NetworkAPI) { | |
this.api = api | |
} | |
} | |
class FileSender(private val filePath: String) : DataSender() { | |
override fun send() { | |
val fileData = File(filePath).readText() | |
api.sendData(fileData) | |
} | |
} | |
class TextSender(private val text: String) : DataSender() { | |
override fun send() { | |
api.sendData(text) | |
} | |
} | |
val fileSender = FileSender("path/to/file.txt") | |
fileSender.setAPI(HTTPAPI()) | |
fileSender.send() | |
val textSender = TextSender("some text data") | |
textSender.setAPI(FTPAPI()) | |
textSender.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment