0

I just read Jonas' well-known article about Cake Pattern, for a class like:

class UserRepository {
  def authenticate(user: User): User = {
    println("authenticating user: " + user)
    user
   }
  def create(user: User) = println("creating user: " + user)
  def delete(user: User) = println("deleting user: " + user)
}

According to that article, for using Cake Pattern, it need to be wrapped as inner class in a trait, e.g.:

trait UserRepositoryComponent {
  val userRepository: UserRepository

  class UserRepository {
    ...
  }
}

So I am wondering:

  1. Is this the only way to achieve Cake Pattern?
  2. If so, does it mean class such as UserRepository has to be designed with Cake Pattern in mind at the time it's designed (so that they can be wrapped in a trait)?
  3. If the answer is yes, is there any common practice that can include a class that is not defined within a wrapper into a design based on Cake Pattern? (something similar to adapter in concept)
Lifu Huang
  • 11,930
  • 14
  • 55
  • 77
  • If this question purely about terminology/definitions or is there any practical component in this question? – SergGr Jan 12 '18 at 08:50
  • The pattern you show there of designing your classes upfront in that specific way is called the cake pattern. So this feels a bit like asking whether you need to implement the cake pattern to achieve Cake Pattern. – Jasper-M Jan 12 '18 at 09:39
  • Hi @SergGr, I think it's both. The first part of my question is for making sure that I understand the definition of Cake Pattern correctly, i.e. does Cake Pattern always require wrapping class such as `UserRepositoryComponent` at the time the class is defined? The second part is, if it is how I understood it, in practical usage, how do people cope with the limitation that I mentioned, i.e. what if I need to use a third party class that is not wrapped in component? Thanks – Lifu Huang Jan 14 '18 at 22:44
  • @Jasper-M, so what you meant is I did understand cake pattern correctly, i.e. cake pattern always require wrapping class at the time it's defined? Then I am wondering, what is the common practice for handling the situation when I need to incorporate a third party class into my classes when it's not wrapped in component? – Lifu Huang Jan 14 '18 at 22:47

0 Answers0