在上一篇讨论里我们简单的介绍了一下Cake Pattern和Reader Monad是如何实现依赖注入的。主要还是从方法上示范了如何用Cake Pattern和Reader在编程过程中解析依赖和注入依赖。考虑到依赖注入模式在编程中的重要性和普遍性,觉着还需要再讨论的深入一些,使依赖注入模式在FP领域里能从理论走向实际。既然我们正在scalaz的介绍系列里,所以这篇我们就着重示范Reader Monad的依赖注入方法。

再说明一下依赖注入:我们说过在团队协作开发里能够实现软件模块的各自独立开发,原理首先是实现软件模块之间的松散耦合(decoupling)。对依赖注入的学术定义如下:Dependency Inversion Principle

1、上层模块不应该依赖下层模块,反之亦然。它们之间不应该有直接的代码引用。它们应该都依赖一个抽象的中间层,也就是共享接口(interface)。

2、抽象层不应该依赖实现细节,而细节实现则必须依赖抽象层。大家都按照共享的抽象层进行实现细节编程。

这两条足够解释何为软件模块松散耦合以及具体编码的要求了。这样来说不仅在团队协同开发,即使在个人独立开发环境下依赖注入模式也能发挥良好的作用。一是可以按需要把软件切分成功能模块独立编程。通过依赖注入模式,当下层模块进行了调整后是不会影响上层模块的;上层模块可以随时连接不同的下层功能模块实现不同的功能,比如连接一些测试环境实现上层模块的独立测试。

下面我们还是取用上期的示范例子,由简入深,逐步说明Reader依赖注入的原理、组合、结构设计:

还说那个咖啡机例子:包括了一个开关设备、咖啡感应器;如果感应到壶里有咖啡的话,按下开关咖啡机可以开启加热。

下面是功能抽象描述,它们是按照开发条件和环境需要进行具体细分的。细分程度要确保每项功能都可以独立完成编程。

   trait OnOffDevice {
def on: Unit
def off: Unit
}
trait SensorDevice {
def isCoffeePresent: Boolean
}

这就是一个抽象层。所有开发人员都必须按照这层的功能描述来编程,所谓编程细节依赖与抽象层要求。

我们现在就可以不用理会以上功能是否已经实现,立即进入上层模块的功能组合了。我们只需要申明依赖项目,先从最简单的开始,假如我现在只需要引用OnOffDevice一项依赖的话,可以在伴生对象(companion object)这样申明操作Reader(primitive reader):

 object OnOffDevice {
def on: Reader[OnOffDevice, String] = Reader(_.on)
def off: Reader[OnOffDevice, String] = Reader(_.off)
}

由于只有一个依赖,我们可以直接申明功能Reader,把on,off两个函数变成Primitive Reader。假如我们需要函数运算结果的话,只要注入OnOffDevice实例。由于Reader是个Monad,我们可以用map这样写:

 object OnOffDevice {
def onOffDevice: Reader[OnOffDevice,OnOffDevice] = Reader(identity)
def on: Reader[OnOffDevice,String] = onOffDevice map { _.on }
def off: Reader[OnOffDevice,String] = onOffDevice map { _.off }
}

我们用identity构建了一个基础(primitive)Reader,然后以这个基础Reader用map组合成我们需要的功能函数Reader。现在的on,off函数款式(signature)和前面的定义是一样的。现在我们可以实现这些Reader功能:

 object OnOffService {
def on = for {
ison <- OnOffDevice.on
} yield ison
def off = for {
isoff <- OnOffDevice.off
} yield isoff
}

我们抽象化了OnOffDevice,不需要实现依赖项目就可以直接使用这些Reader功能函数:

 ef trigger = OnOffService.on      //> trigger: => scalaz.Kleisli[scalaz.Id.Id,OnOffDevice,String]

假如现在实现了OnOffDevice实例:

 class OnOffDeviceImpl extends OnOffDevice {
def on = "MockDevice.On"
def off = "MockDevice.Off"
}

我们可以在最终运行中注入实现的依赖实例来获取最终结果:

 object MockOnOffDevice extends OnOffDeviceImpl
def trigger = OnOffService.on //> trigger: => scalaz.Kleisli[scalaz.Id.Id,Exercises.reader1.OnOffDevice,String
//| ]
val result = trigger(MockOnOffDevice) //> result : scalaz.Id.Id[String] = SomeDevice.On
result === "SomeDevice.On" //> res0: Boolean = true

我们看到,对运算结果注入依赖实例后就能得出具体的运算值。

那如果我们需要两项依赖呢?

 trait OnOffDevice {
def on: String
def off: String
}
trait SensorDevice {
def isCoffeePresent: Boolean
}

我们可以把两个依赖组成一个更大的功能,一个更高一层的依赖:

 rait Device { //高层组合依赖
def onOffDevice: OnOffDevice //具体依赖
def sensorDevice: SensorDevice //具体依赖
}
object Device {
val device = Reader[Device,Device](identity)
val onOffDevice = device map {_.onOffDevice}
val sensorDevice = device map {_.sensorDevice}
}

所有Reader的注入依赖现在变成Device类型了。依赖的功能Reader变成这样:

 object OnOffDevice {
import Device.onOffDevice
def on: Reader[Device,String] = onOffDevice map { _.on }
def off: Reader[Device,String] = onOffDevice map { _.off }
}
object SensorDevice {
import Device.sensorDevice
def isCoffeePresent: Reader[Device,Boolean] = sensorDevice map { _.isCoffeePresent }
}

我们看到原来的Reader实现细节编码是不需要改变的,如OnOffService。
假设我们获得了下面的功能实现程序:

 class OnOffDeviceImpl extends OnOffDevice {
def on = "SomeDevice.On"
def off = "SomeDevice.Off"
}
class SensorDeviceImpl extends SensorDevice {
def isCoffeePresent = true
}

由于这次的依赖注入是Device类型的,所以我们需要获取Device实例用来注入到结果Reader:

 object MockOnOffDevice extends OnOffDeviceImpl
object MockSensorDevice extends SensorDeviceImpl
trait DeviceImpl extends Device {
def onOffDevice = new OnOffDevice {
def on = MockOnOffDevice.on
def off = MockOnOffDevice.off
}
def sensorDevice = new SensorDevice {
def isCoffeePresent = MockSensorDevice.isCoffeePresent
}
}

现在这个DeviceImpl是一个Device实例,我们可以把它注入到Reader得出运算结果,还是先运行上面的例子:

 object MockDevice extends Device with DeviceImpl
def trigger = OnOffService.on //> trigger: => scalaz.Kleisli[scalaz.Id.Id,Exercises.reader2.Device,String]
val result = trigger(MockDevice) //> result : scalaz.Id.Id[String] = SomeDevice.On

这次是用Device实例注入得出结果的。

我们还可以把依赖项目独立分别定义,这样可以更灵活的组合Device:

 trait OnOffComponent {
def onOffDevice: OnOffDevice
}
trait SensorComponent {
def sensorDevice: SensorDevice
}
trait Device extends OnOffComponent with SensorComponent

这样我们可以把两个依赖分开来格式化后组合成Device实例,因为我们的注入依赖现在是Device类型的了:

 object MockOnOffDevice extends OnOffDeviceImpl
object MockSensorDevice extends SensorDeviceImpl
trait OnOffFunctions extends OnOffComponent {
def onOffDevice = MockOnOffDevice
}
trait SensorFunctions extends SensorComponent {
def sensorDevice = MockSensorDevice
}

完成了Device实例的组合后可以通过注入Device实例来得取运算结果:

 object MockDevice extends Device with OnOffFunctions with SensorFunctions
def trigger =
if (SensorService.isCoffeePresent(MockDevice))
OnOffService.on(MockDevice)
else
OnOffService.off(MockDevice) //> trigger: => scalaz.Id.Id[String]
trigger //> res0: scalaz.Id.Id[String] = SomeDevice.On

如果换另一个版本的SensorDevice实现:

 class SensorDeviceImpl extends SensorDevice {
def isCoffeePresent = false
}

重新运行:

 object MockDevice extends Device with OnOffFunctions with SensorFunctions
def trigger =
if (SensorService.isCoffeePresent(MockDevice))
OnOffService.on(MockDevice)
else
OnOffService.off(MockDevice) //> trigger: => scalaz.Id.Id[String]
trigger //> res0: scalaz.Id.Id[String] = SomeDevice.Off

正确反应了新的运算结果。

从以上的示范中我们看到了依赖的层次结构以及Reader搭配。我们可以用多层结构来精简基础Reader。但多层式的依赖结构统一了注入依赖类型,最后注入时就无法拆分依赖类型,又会弱化依赖的组合灵活性。所以在组织依赖时应该注意确定在自己的程序中将会使用到所有依赖。这样调用统一的一种注入类型就足够了。

下面再增添多一个依赖:增加一个电源制式检测功能,只有US制式的电源才能启动咖啡机。现在全部功能的抽象描述如下:

 trait OnOffDevice {
def on: String
def off: String
}
trait SensorDevice {
def isCoffeePresent: Boolean
}
trait PowerConfig {
def getPowerVolts(country: String): Int
def isUSStandard(volt: Int): Boolean
}

我们再试试增加多一层结构:

 trait Device extends OnOffComponent with SensorComponent
trait DeviceComponent {
def onOffDevice: OnOffDevice
def sensorDevice: SensorDevice
}
trait PowerComponent {
def powerConfig: PowerConfig
}
trait Appliance extends DeviceComponent with PowerComponent
object Appliance {
val appliance = Reader[Appliance,Appliance](identity)
val onOffDevice = appliance map {_.onOffDevice}
val sensorDevice = appliance map {_.sensorDevice}
val powerConfig = appliance map {_.powerConfig}
}

Appliance是更高层依赖。

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

 object OnOffDevice {
import Appliance.onOffDevice
def on: Reader[Appliance,String] = onOffDevice map { _.on }
def off: Reader[Appliance,String] = onOffDevice map { _.off }
}
object SensorDevice {
import Appliance.sensorDevice
def isCoffeePresent: Reader[Appliance,Boolean] = sensorDevice map { _.isCoffeePresent }
}
object PowerConfig {
import Appliance.powerConfig
def getPowerVolts(country: String) = powerConfig map {_.getPowerVolts(country)}
def isUSStandard(volts: Int) = powerConfig map {_.isUSStandard(volts)}
}

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

 object OnOffService {
def on = for {
ison <- OnOffDevice.on
} yield ison
def off = for {
isoff <- OnOffDevice.off
} yield isoff
}
object SensorService {
def isCoffeePresent = for {
hasCoffee <- SensorDevice.isCoffeePresent
} yield hasCoffee
}
object PowerService {
def isUSStandard(country: String) = for {
is110v <- PowerConfig.getPowerVolts(country)
isUSS <- PowerConfig.isUSStandard(is110v)
} yield isUSS
}

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

 class OnOffDeviceImpl extends OnOffDevice {
def on = "SomeDevice.On"
def off = "SomeDevice.Off"
}
class SensorDeviceImpl extends SensorDevice {
def isCoffeePresent = true
}
class PowerConfigImpl extends PowerConfig {
def getPowerVolts(country: String) = country match {
case "USA" =>
case "UK" =>
case "HK" =>
case "CHN" =>
case _ =>
}
def isUSStandard(volts: Int) = volts ===
}

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

 object MockOnOffDevice extends OnOffDeviceImpl
object MockSensorDevice extends SensorDeviceImpl
object MockPowerConfig extends PowerConfigImpl
trait OnOffFunctions extends OnOffComponent {
def onOffDevice = MockOnOffDevice
}
trait SensorFunctions extends SensorComponent {
def sensorDevice = MockSensorDevice
}
trait DeviceFunctions extends DeviceComponent {
def onOffDevice = MockOnOffDevice
def sensorDevice = MockSensorDevice
}
trait PowerFunctions extends PowerComponent {
def powerConfig = MockPowerConfig
}

再直接进行Appliance实例组合:

 object MockAppliance extends Appliance with DeviceFunctions with PowerFunctions

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

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

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

 package Exercises
import scalaz._
import Scalaz._
object reader3 {
trait OnOffDevice {
def on: String
def off: String
}
trait SensorDevice {
def isCoffeePresent: Boolean
}
trait PowerConfig {
def getPowerVolts(country: String): Int
def isUSStandard(volt: Int): Boolean
} trait OnOffComponent {
def onOffDevice: OnOffDevice
}
trait SensorComponent {
def sensorDevice: SensorDevice
}
trait Device extends OnOffComponent with SensorComponent
trait DeviceComponent {
def onOffDevice: OnOffDevice
def sensorDevice: SensorDevice
}
trait PowerComponent {
def powerConfig: PowerConfig
}
trait Appliance extends DeviceComponent with PowerComponent
object Appliance {
val appliance = Reader[Appliance,Appliance](identity)
val onOffDevice = appliance map {_.onOffDevice}
val sensorDevice = appliance map {_.sensorDevice}
val powerConfig = appliance map {_.powerConfig}
}
object OnOffDevice {
import Appliance.onOffDevice
def on: Reader[Appliance,String] = onOffDevice map { _.on }
def off: Reader[Appliance,String] = onOffDevice map { _.off }
}
object SensorDevice {
import Appliance.sensorDevice
def isCoffeePresent: Reader[Appliance,Boolean] = sensorDevice map { _.isCoffeePresent }
}
object PowerConfig {
import Appliance.powerConfig
def getPowerVolts(country: String) = powerConfig map {_.getPowerVolts(country)}
def isUSStandard(volts: Int) = powerConfig map {_.isUSStandard(volts)}
}
object OnOffService {
def on = for {
ison <- OnOffDevice.on
} yield ison
def off = for {
isoff <- OnOffDevice.off
} yield isoff
}
object SensorService {
def isCoffeePresent = for {
hasCoffee <- SensorDevice.isCoffeePresent
} yield hasCoffee
}
object PowerService {
def isUSStandard(country: String) = for {
is110v <- PowerConfig.getPowerVolts(country)
isUSS <- PowerConfig.isUSStandard(is110v)
} yield isUSS
}
class OnOffDeviceImpl extends OnOffDevice {
def on = "SomeDevice.On"
def off = "SomeDevice.Off"
}
class SensorDeviceImpl extends SensorDevice {
def isCoffeePresent = true
}
class PowerConfigImpl extends PowerConfig {
def getPowerVolts(country: String) = country match {
case "USA" =>
case "UK" =>
case "HK" =>
case "CHN" =>
case _ =>
}
def isUSStandard(volts: Int) = volts ===
}
object MockOnOffDevice extends OnOffDeviceImpl
object MockSensorDevice extends SensorDeviceImpl
object MockPowerConfig extends PowerConfigImpl
trait OnOffFunctions extends OnOffComponent {
def onOffDevice = MockOnOffDevice
}
trait SensorFunctions extends SensorComponent {
def sensorDevice = MockSensorDevice
}
trait DeviceFunctions extends DeviceComponent {
def onOffDevice = MockOnOffDevice
def sensorDevice = MockSensorDevice
}
trait PowerFunctions extends PowerComponent {
def powerConfig = MockPowerConfig
}
object MockAppliance extends Appliance with DeviceFunctions with PowerFunctions
def trigger =
if ((PowerService.isUSStandard("CHN")(MockAppliance))
&& (SensorService.isCoffeePresent(MockAppliance)))
OnOffService.on(MockAppliance)
else
OnOffService.off(MockAppliance) //> trigger: => scalaz.Id.Id[String]
trigger //> res0: scalaz.Id.Id[String] = SomeDevice.On
}

Scalaz(16)- Monad:依赖注入-Dependency Injection By Reader Monad的更多相关文章

  1. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  2. 简明依赖注入(Dependency Injection)

    前言 这是因特奈特上面不知道第几万篇讲依赖注入(Dependency Injection)的文章,但是说明白的却寥寥无几,这篇文章尝试控制字数同时不做大多数. 首先,依赖注入的是一件很简单的事情. 为 ...

  3. 14.AutoMapper 之依赖注入(Dependency Injection)

    https://www.jianshu.com/p/f66447282780   依赖注入(Dependency Injection) AutoMapper支持使用静态服务定位构建自定义值解析器和自定 ...

  4. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

  5. Spring点滴七:Spring中依赖注入(Dependency Injection:DI)

    Spring机制中主要有两种依赖注入:Constructor-based Dependency Injection(基于构造方法依赖注入) 和 Setter-based Dependency Inje ...

  6. 设计模式之————依赖注入(Dependency Injection)与控制反转(Inversion of Controller)

    参考链接: 依赖注入(DI) or 控制反转(IoC) laravel 学习笔记 —— 神奇的服务容器 PHP 依赖注入,从此不再考虑加载顺序 名词解释 IoC(Inversion of Contro ...

  7. 理解依赖注入(Dependency Injection)

    理解依赖注入 Yii2.0 使用了依赖注入的思想.正是使用这种模式,使得Yii2异常灵活和强大.千万不要以为这是很玄乎的东西,看完下面的两个例子就懂了. class SessionStorage { ...

  8. AngularJS - 依赖注入(Dependency Injection)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 依赖注入 依赖注入是软件设计模式中的一部分,用于处理组件是如何得到它说依赖的其它组件的. ...

  9. Spring之对象依赖关系(依赖注入Dependency Injection)

    承接上篇: Spring中,如何给对象的属性赋值: 1:通过构造函数,如下所示: <!-- 1:构造函数赋初始值 --><bean id="user1" clas ...

随机推荐

  1. 关于STM32的外部引脚中断的问题

    今天想用自己以前的比较干净的工程模板做一个东西,,,,,,,在添加上引脚中断的时候,,突然想知道自己配置的中断优先级是否正确执行,,,,, 以前刚学习32的时候测试过是可以的,,不过今天发现了一个大问 ...

  2. Atitit 发帖机系列(8)  词法分析器v5 版本新特性说明)

    Atitit 发帖机系列(8)  词法分析器v5 版本新特性说明) v5  增加对sql单引号的内部支持.可以作为string 结构调整,使用递归法重构循环发..放弃循环发. V4 java dsl词 ...

  3. js算法之最常用的排序

    引入 大学学习计算机语言的那几年,从c语言,到c++,再到数据结构JAVA..让我印象最深刻的还是最开始老师讲冒泡算法的时候,直到现在大四快毕业了我才渐渐通窍了.刚学前端的时候以为前端就是做出好看很炫 ...

  4. javaweb回顾第十一篇过滤器(附实现中文乱码问题)

    1:过滤器概念 过滤器就是一种在请求目标资源的中间组件,比喻把污水转换成纯净水中间需要一个污水净化设备,那么这个设备就好比一个过滤器.那么我用图来表示过滤器(可以有多个过滤器)运行的过程 2:Filt ...

  5. 快速入门系列--WebAPI--01基础

    ASP.NET MVC和WebAPI已经是.NET Web部分的主流,刚开始时两个公用同一个管道,之后为了更加的轻量化(WebAPI是对WCF Restful的轻量化),WebAPI使用了新的管道,因 ...

  6. CSS技能汇总,研究及实践

    最近一直在研究CSS,因为发现实践中大部分时间都在写CSS,且自己感觉写的很烂,虽然以前看的很多,但却很少有去实践过,更别提研究了,现在发现根本就不是你懂你就会,很多都是你用着用着才真的会了的,于是现 ...

  7. 找到SQL Server数据库历史增长信息

        很多时候,在我们规划SQL Server数据库的空间,或向存储方面要空间时,都需要估算所需申请数据库空间的大小,估计未来最简单的办法就是看过去的趋势,这通常也是最合理的方式.     通常来讲 ...

  8. Bmob基础

    BaaS是指专为移动应用开发者提供整合云后端的服务.开发者无需过多研究服务器端程序,而只需调用云计算平台提供的API,使用相应SDK,就能迅速完成数据存储.账户管理.消息推送.社交网络整合等功能.本文 ...

  9. javase基础复习攻略《二》

    今天就开始的真正走进JAVASE的世界,本篇介绍的是:JAVASE基础语法,大家如果有C语言的基础,对于本节内容一定感觉非常轻松,编程语言之间的都是相通的,只不过C语言属于面向过程编程,而JAVA语言 ...

  10. HashSet中实现不插入重复的元素

    /* 看一下部分的HashSet源码.... public class HashSet<E> extends AbstractSet<E> implements Set< ...