设为首页 加入收藏

TOP

Scalaz(16)- Monad:依赖注入-Dependency Injection By Reader Monad(三)
2017-10-10 12:13:30 】 浏览:7258
Tags:Scalaz Monad 依赖 注入 Dependency Injection Reader
= appliance map {_.sensorDevice} 14 val powerConfig = appliance map {_.powerConfig} 15 }

Appliance是更高层依赖。

下面还是保留了原来的Reader实现编码不变,除了import:

 1 object OnOffDevice {  2 import Appliance.onOffDevice  3     def on: Reader[Appliance,String] = onOffDevice map { _.on }  4     def off: Reader[Appliance,String] = onOffDevice map { _.off }  5 }  6 object SensorDevice {  7 import Appliance.sensorDevice  8   def isCoffeePresent: Reader[Appliance,Boolean] = sensorDevice map { _.isCoffeePresent }  9 } 10 object PowerConfig { 11 import Appliance.powerConfig 12     def getPowerVolts(country: String) = powerConfig map {_.getPowerVolts(country)} 13     def isUSStandard(volts: Int) = powerConfig map {_.isUSStandard(volts)} 14 }

现在注入依赖的类型变成了Appliance。原来Reader功能实现代码还是不用改变:

 1 object OnOffService {  2     def on = for {  3         ison <- OnOffDevice.on  4     } yield ison  5     def off = for {  6         isoff <- OnOffDevice.off  7     } yield isoff  8 }  9 object SensorService { 10     def isCoffeePresent = for { 11         hasCoffee <- SensorDevice.isCoffeePresent 12     } yield hasCoffee 13 } 14 object PowerService { 15     def isUSStandard(country: String) = for { 16         is110v <- PowerConfig.getPowerVolts(country) 17         isUSS <- PowerConfig.isUSStandard(is110v) 18     } yield isUSS 19 }

假如增加了新功能实现程序:

 1 class OnOffDeviceImpl extends OnOffDevice {  2     def on = "SomeDevice.On"
 3     def off = "SomeDevice.Off"
 4 }  5 class SensorDeviceImpl extends SensorDevice {  6     def isCoffeePresent = true
 7 }  8 class PowerConfigImpl extends PowerConfig {  9     def getPowerVolts(country: String) = country match { 10         case "USA" => 110
11         case "UK" => 220
12         case "HK" => 220
13         case "CHN" => 110
14         case _  => 0
15  } 16     def isUSStandard(volts: Int) = volts === 110
17 }

同样,需要把这些实例转成Appliance类型: 

 1 object MockOnOffDevice extends OnOffDeviceImpl  2 object MockSensorDevice extends SensorDeviceImpl  3 object MockPowerConfig extends PowerConfigImpl  4 trait OnOffFunctions extends OnOffComponent {  5     def onOffDevice = MockOnOffDevice  6 }  7 trait SensorFunctions extends SensorComponent {  8   def sensorDevice = MockSensorDevice  9 } 10 trait DeviceFunctions extends DeviceComponent { 11     def onOffDevice = MockOnOffDevice 12   def sensorDevice = MockSensorDevice 13 } 14 trait PowerFunctions extends PowerComponent { 15     def powerConfig = MockPowerConfig 16 }

再直接进行Appliance实例组合:

1 object MockAppliance extends Appliance with DeviceFunctions with PowerFunctions

运行后注入Appliance实例得出结果:

1 def trigger =
2   if ((PowerService.isUSStandard("CHN")(MockAppliance)) 3       && (SensorService.isCoffeePresent(MockAppliance))) 4  OnOffService.on(MockAppliance) 5    else
6      OnOffService.off(MockAppliance)              //> trigger: => scalaz.Id.Id[String]
7 trigger                                           //> res0: scalaz.Id.Id[String] = SomeDevice.On

下面是这段程序的源代码,提供给大家作为参考:

 1 package Exercises  2 import scalaz._  3 import Scalaz._  4 object reader3 {  5 trait OnOffDevice {  6  def on: String  7  def off: String  8 }  9 trait SensorDevice {  10  def isCoffeePresent: Boolean  11 }  12 trait PowerConfig {  13  def getPowerVolts(country: String): Int  14  def isUSStandard(volt: Int): Boolean  15 }  16 
 17 trait OnOffComponent {  18  def onOffDevice: OnOffDevice  19 }  20 trait SensorComponent {  21  def sensorDevice: SensorDevice  22 }  23 trait Device extends OnOffComponent with SensorComponent  24 trait DeviceComponent {  25  def onOffDevice: OnOffDevice  26  def sensorDevice: SensorDevice  27 }  28 trait PowerComp
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Scalaz(15)- Monad:依赖注入.. 下一篇Scalaz(17)- Monad:泛函状态..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目