Created
November 20, 2011 07:35
-
-
Save momania/1379932 to your computer and use it in GitHub Desktop.
CoffeePot Cake
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
object CoffeePot extends App { | |
ComponentRegistry.coffeeWarmer.trigger | |
} | |
trait OnOffDevice { | |
def on: Unit | |
def off: Unit | |
} | |
trait SensorDevice { | |
def isCoffeePresent: Boolean | |
} | |
trait Heater extends OnOffDevice { | |
def on = println("heater.on") | |
def off = println("heater.off") | |
} | |
trait PotSensor extends SensorDevice { | |
def isCoffeePresent = true | |
} | |
trait Warmer { | |
this: OnOffDevice with SensorDevice => | |
def trigger = { | |
if (isCoffeePresent) on | |
else off | |
} | |
} | |
object ComponentRegistry { | |
val coffeeWarmer = new Warmer with PotSensor with Heater | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment