mercredi 28 décembre 2016

Difference between using Cake pattern and functions in Scala - why is the Cake pattern useful?

I was wondering about the difference between using functions and Cake pattern for DI in Scala. I came up with the following understanding(s), I would like to know if this understanding is correct.

Let's imagine a dependency graph.

1) If we use functions as building blocks then the graph consists of functions as nodes and parameters as edges.

2) If we use traits as building blocks (as in Cake) then the graph consists of traits as nodes and abstract members as edges.

So what is the purpose of Cake pattern ? Why is 2 better than 1 ? It is course graining. Graph-1 can be simplified by grouping functions into traits and then we have a smaller, more understandable Graph-2.

Does this make sense ? I am trying to understand why the Cake pattern is useful. The grouping/clustering of related concepts is a form of compression and creates understanding (we need to hold less things in our head to get an understanding).

Here is a different comparison (between Cake vs package system):

Cake is similar to grouping related functions into packages but it goes beyond that because using name-spaces (packages/objects) causes dependencies to be hard-wired, Cake is replacing packages/objects with traits and imports with self type annotations/abstract members. The difference between packages and Cake pattern is that the actual implementation of a dependency can change using Cake while it CANNOT change when using packages.

I don't know if these analogies do make sense or not, if not please correct me, if yes, please reassure me. I am still trying to wrap my head around the Cake pattern and how to relate it to concepts that I already understand (functions, packages).

Aucun commentaire:

Enregistrer un commentaire