Last active
August 29, 2015 14:02
-
-
Save freewind/02796ae9078b82ee6ef9 to your computer and use it in GitHub Desktop.
Cake pattern example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(username:String, password: String) | |
trait UserRepositoryComponent { | |
val userRepository: UserRepository | |
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) | |
} | |
} | |
trait UserServiceComponent { this: UserRepositoryComponent => | |
val userService: UserService | |
class UserService { | |
def authenticate(username: String, password: String): User = | |
userRepository.authenticate(User(username, password)) | |
def create(username: String, password: String) = | |
userRepository.create(new User(username, password)) | |
def delete(user: User) = | |
userRepository.delete(user) | |
} | |
} | |
object ComponentRegistry extends | |
UserServiceComponent with | |
UserRepositoryComponent | |
{ | |
val userRepository = new UserRepository | |
val userService = new UserService | |
} | |
/* | |
// In testing, I can provided some mocks for the components | |
object TestingComponentRegistry extends | |
UserServiceComponent with | |
UserRepositoryComponent | |
{ | |
val userRepository = mock[UserRepository] | |
val userService = mock[UserService] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment