Last active
September 17, 2017 13:50
-
-
Save cwdoh/bcb4516c0ae5895a2c7890de3c5b324c to your computer and use it in GitHub Desktop.
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 Heater { | |
fun on() | |
fun off() | |
fun isHot() : Boolean | |
} | |
class ElectricHeater(var heating: Boolean = false) : Heater { | |
override fun on() { | |
println("~ ~ ~ heating ~ ~ ~") | |
heating = true | |
} | |
override fun off() { | |
heating = false | |
} | |
override fun isHot() : Boolean { | |
return heating | |
} | |
} | |
interface Pump { | |
fun pump() | |
} | |
class Thermosiphon(heater: Heater) : Pump, Heater by heater { | |
override fun pump() { | |
if (isHot()) { | |
println("=> => pumping => =>"); | |
} | |
} | |
} | |
interface CoffeeModule { | |
fun getThermosiphon() : Thermosiphon | |
} | |
class MyDripCoffeeModule : CoffeeModule { | |
companion object { | |
val electricHeater: ElectricHeater by lazy { | |
ElectricHeater() | |
} | |
} | |
private val _thermosiphon : Thermosiphon by lazy { | |
Thermosiphon(electricHeater) | |
} | |
override fun getThermosiphon() : Thermosiphon = _thermosiphon | |
} | |
class CoffeeMaker(val coffeeModule: CoffeeModule) { | |
fun brew() { | |
coffeeModule.getThermosiphon().run { | |
on() | |
pump() | |
println(" [_]P coffee! [_]P ") | |
off() | |
} | |
} | |
} | |
fun main(args: Array<String>) { | |
val coffeeMaker = CoffeeMaker(MyDripCoffeeModule()) | |
coffeeMaker.brew(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment