Does Cake Pattern always need to be implemented as inner class?

54 Views Asked by At

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)
0

There are 0 best solutions below