1 trait Heater extends OnOffDevice { 2 def on = println("heater.on") 3 def off = println("heater.off") 4 } 5 trait PotSensor extends SensorDevice { 6 def isCoffeePresent = false
7 }
1 object allDevices extends Heater with PotSensor
1 def trigger = { 2 if ( WarmerFuncImpl.thereIsCoffee(allDevices) ) 3 WarmerFuncImpl.warmerOn(allDevices) 4 else
5 WarmerFuncImpl.warmerOff(allDevices) 6 } //> trigger: => scalaz.Id.Id[Unit] 7 //测试运行
8 trigger //> heater.off
1 trait PotHasCoffee extends SensorDevice { 2 def isCoffeePresent = true
3 }
1 object allDevices extends Heater with PotHasCoffee
1 //测试运行
2 trigger //> heater.on