看过一些关于Scala的蛋糕模式的文章,基本明白了。以下是我从本文中复制的一些示例代码:
成分:
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
}
object TestingComponentRegistry extends
UserServiceComponent with
UserRepositoryComponent {
val userRepository = mock[UserRepository]
val userService = mock[UserService]
}
如果我在一个简单的项目中,我想让它更简单。代码将如下所示:
case class User(username:String, password: String)
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)
}
class UserService(userRepository: UserRepository) {
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 Application {
val userService = new UserService(new UserRepository)
}
object Test {
val userService = new UserService(mock[UserRepository])
}
我的问题是,我的代码仍然可以被视为“依赖注入”吗?
UserService
我在的构造函数中声明了依赖项- 我将它们与不同环境中的对象结合起来
但我没有提供一些特征作为“组件”。