Dependency Injection Using Cake Pattern and Transfer Parameters in Constructor

After reading this blog post, I do not understand:

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

In other words, what is 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 (outside the syntax)? (Must have otherwise, self annotations will not exist).

EDIT:

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

This is also important, so in principle there is no difference between the two styles?

+4

Source: https://habr.com/ru/post/1652889/


All Articles