Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Printer {
- fun print(data: String)
- class Log : Printer {
- override fun print(data: String) {
- Log.d("TAG","Print some info to log - $data")
- }
- }
- class Firebase(private val firebaseAnalytics: FirebaseAnalytics) : Printer {
- override fun print(data: String) {
- Log.d("TAG","Firebase data print - $data")
- }
- }
- class Empty : Printer {
- override fun print(data: String) = Unit
- }
- }
- interface Data {
- fun changePrinter(printer: Printer)
- fun print()
- class Base(
- private val data: String
- ) : Data {
- private var printer: Printer = Printer.Empty
- override fun changePrinter(printer: Printer) {
- this.printer = printer
- }
- override fun print() = printer.print(data)
- }
- }
- fun main() {
- // Using Strategy pattern
- val data = Data.Base("Data from network")
- val logPrinter = Printer.Log()
- data.changePrinter(logPrinter)
- data.printData() // print the following text: Print some info to log - Data from network
- val firebasePrinter = Printer.Firebase(Firebase.getAnalytics())
- data.changePrinter(firebasePrinter)
- data.printData() // print the following text: Firebase data print - Data from network
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement