Cake Pattern and Self-Type Annotation

Dear all,

My impression about the Cake Pattern has always been that it builds on top of self-type annotation. Is that right? Or, can one still say that the pattern is followed even though there is no self-type annotation.

One more thing: Is there any definitive documentation of the pattern? I’m only interested in documents that are academically respectable. The official Scala website is also fine.

TIA,
–Hossein

  • Probably the best coverage of the cake design pattern that I’ve ever seen can be found in the pages of the superb book entitled Scala Design Patterns by Ivan Nikolov (Packt Publishing).
  • As Ivan nicely describes it, “The cake design pattern is the Scala way to implement dependency injection. It is something that is used quite a lot in real-life applications, and there are numerous libraries that help developers achieve it. Scala has a way of doing this using language features, and this is what the cake design pattern is all about.”

Thank you Akram for the reference. Here are a few excerpts from the book on the subject of my question:

“Self types are often used for dependency injection. They are the main part of the Cake design pattern…”

“The cake design pattern relies completely on self types.”

He, later, considers implicits and Reader Monads as alternatives to the Cake Pattern for dependency injection. Yet, I see nothing in the book which states whether or not one is still considered to follow the Cake Pattern itself without employing self-types.

And, with all due respect to Ivan Nikolov, I’d like to know how authentic this book is considered to be in the community? I see it is not mentioned in the official Scala books page:

http://docs.scala-lang.org/books.html

  • Sure thing, Hossein, I’m always glad to help :sunglasses:
  • I’m delighted that you’re reading the book with great alertness and care to details. I will defer to Scala gurus to weigh in to help answer your excellent (follow up) questions.
  • Overall, I’m happy and satisfied with the book I mentioned, which is entitled Scala Design Patterns by Ivan Nikolov. It covers a lot of ground, without getting bogged down. By the same token, your questions to get further clarification are commendable.
  • I, too, will stay tuned as hopefully another Scala can weigh in :scala:

Dear all,

Using John Hunt’s Scala Design Patterns [1], I realised that the cake pattern is rooted in a 2005 work of Odersky and Zenger [2]. Here is the openning of their abstract:

We identify three programming language abstractions for the construction of reusable components: abstract type members, explicit selftypes, and modular mixin composition.

So, the parents of the cake pattern clearly consider self-type annotation an essential constituent of their baby. My next question would then be: Do Scala people still have the same mindset?

Cheers,
–Hossein

[1] John Hunt, Scala Design Patterns: Patterns for Practical Reuse and Design, DOI 10.1007/978-3-319-02192-8_14, Springer International Publishing Switzerland, 2013

[2] Martin Odersky, Matthias Zenger, Scalable Component Abstractions. OOPSLA 2005: 41-57

  • Thanks for sharing the helpful, detailed note. It’s heartening to see that books on Scala design patterns are become increasingly popular :moneybag:
  • I had, of course, mentioned the superb book entitled Scala Design Patterns by Ivan Nikolov at the outset. But wasn’t aware of the book you mention, the one by John Hunt, which I’ll definitely be checking out soon!