我还在尝试学习Scala的Cake模式。在我看来,它为您提供了集中配置“组件”的优势,以及为这些组件提供默认实现的能力(当然,这些组件是可覆盖的)。
然而,它使用自类型特征来描述依赖关系,似乎混合了关注的领域。组件的目的(我认为)是抽象出该组件的不同实现。但是组件中描述的依赖项列表本身就是一个实现问题。
例如,假设我有一个充满小部件的数据库,一个允许我查找特定类型小部件的注册表,以及一些使用注册表处理小部件的算法:
case class Widget(id: Int, name:String)
trait DatabaseComponent {
  def database: (Int => Widget) = new DefaultDatabase()
  class DefaultDatabase extends (Int => Widget) {
    // silly impl
    def apply(x: Int) = new Person(x, "Bob")
  }
}
trait RegistryComponent {
  this: DatabaseComponent =>  // registry depends on the database
  def registry: (List[Int] => List[Widget]) = new DefaultRegistry()
  class DefaultRegistry extends (List[Int] => List[Widget]) {
    def apply(xs: List[Int]) = xs.map(database(_))
  }
}
trait AlgorithmComponent {
  this: RegistryComponent =>  // algorithm depends on the registry
  def algorithm: (() => List[Widget]) = new DefaultAlgorithm()
  class DefaultAlgorithm extends (() => List[Widget]) {
    // look up employee id's somehow, then feed them
    // to the registry for lookup
    def apply: List[Widget] = registry(List(1,2,3))
  }
}现在,您可以将其放在某个中央配置中:
object Main {
  def main(args: Array[String]) {
    val algorithm = new AlgorithmComponent() with RegistryComponent with DatabaseComponent
    val widgets = println("results: " + algorithm.processor().mkString(", "))
  }
}如果我想要更改到不同的数据库,我可以通过更改我的混合来轻松地注入它:
val algorithm = new AlgorithmComponent() with RegistryComponent with SomeOtherDatabaseComponentBut...what如果我想混入一个不使用数据库的不同注册表组件?
如果我试图用一个不同的(非默认)实现对RegistryComponent进行子类化,RegistryComponent将坚持要求我包含一个DatabaseComponent依赖项。我必须使用RegistryComponent,因为这是顶级AlgorithmComponent所需要的。
我是不是遗漏了什么?当我在任何组件中使用self类型时,我声明所有可能的实现都必须使用这些相同的依赖项。
有没有其他人遇到过这个问题?什么是像蛋糕一样解决它的方法?
谢谢!
https://stackoverflow.com/questions/9621183
复制相似问题