Skip to content

Instantly share code, notes, and snippets.

@momania
Created November 20, 2011 07:35
Show Gist options
  • Save momania/1379932 to your computer and use it in GitHub Desktop.
Save momania/1379932 to your computer and use it in GitHub Desktop.
CoffeePot Cake
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