Advertisement
Kostiggig

Decorator

Jul 10th, 2022
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.07 KB | None | 0 0
  1. fun main() {
  2.  
  3.     val printer = Printer.Base()
  4.     val milk = Product.Milk.Base(60)
  5.     val milkWithDiscount = Product.Milk.MilkWithDiscount(milk)
  6.  
  7.     val milkWithWithDiscount = Product.Milk.MilkWithDiscount(
  8.         milkWithDiscount
  9.     )
  10.    
  11.     milk.printPrice(printer)
  12.     milkWithDiscount.printPrice(printer)
  13.     milkWithWithDiscount.printPrice(printer)
  14. }
  15.  
  16. interface Product {
  17.  
  18.     fun printPrice(printer: Printer)
  19.  
  20.     fun price() : Int
  21.  
  22.  
  23.     interface Milk : Product {
  24.  
  25.         class Base(private val price: Int) : Milk {
  26.  
  27.             override fun printPrice(printer: Printer) = printer.print(price)
  28.  
  29.             override fun price() : Int = price
  30.         }
  31.  
  32.         class MilkWithDiscount(private val milk: Milk) : Milk {
  33.             override fun printPrice(printer: Printer) = printer.print(price())
  34.  
  35.             override fun price() : Int = milk.price() - 20
  36.         }
  37.     }
  38. }
  39.  
  40. interface Printer {
  41.  
  42.     fun print(value: Int)
  43.  
  44.     class Base : Printer {
  45.         override fun print(value: Int) = println("Price is value $value")
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement