Last active
July 28, 2025 11:20
-
-
Save taq/84bf0ee20678ff220de94f99edd15217 to your computer and use it in GitHub Desktop.
Kotlin: Injetando um Logger em Person
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 Logger { | |
| abstract fun log(msg: String) | |
| } | |
| class ConsoleLogger : Logger { | |
| override fun log(msg: String) { | |
| println("Log: $msg") | |
| } | |
| } | |
| class FileLogger : Logger { | |
| override fun log(msg: String) { | |
| println("Log to file: $msg") | |
| } | |
| } | |
| class Person(val name: String, val logger: Logger) { | |
| fun hello() { | |
| logger.log("Hello, $name!") | |
| } | |
| fun bye() { | |
| logger.log("Bye!") | |
| } | |
| } | |
| fun main() { | |
| val console = ConsoleLogger() | |
| val file = FileLogger() | |
| val person = Person("tag", console) | |
| person.hello() | |
| person.bye() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment