Dependency injection using Cake Pattern vs. passing parameters in constructor

671 Views Asked by At

After reading this blog post I don't understand:

What is the difference between using self type annotations vs. specifying constructor parameters for dependency injection ?

In other words, whats the difference between this style:

object Main {
  def main(args: Array[String]) {
    val barWithFoo = new BarUsingFooAble with MyFooAble with BazAble
    println(barWithFoo.bar())
  }
}

and this:

object Main {
  def main(args: Array[String]) {
    val barWithFooAndBaz = new BarUsingFooAble(new FooAble with BazAble)
    println(barWithFooAndBaz.bar())
  }
}

and this (third option):

object Main {
  def main(args: Array[String]) {
    val barWithFoo = new BarUsingFooAble with FooAbleComponent with BazAbleComponent {
      val bazAble = new BazAble() //or any other implementation
      val fooAble = new FooAble() //or any other implementation
    }
    println(barWithFoo.bar())
  }
}

?

Is there any (beyond syntax)? (There must be, otherwise self type annotations would not exist).

EDIT:

This seems to be related question, although it does not answer this question.

This is relevant too, so basically there is no difference between the two styles at all ?

0

There are 0 best solutions below