Created
September 3, 2009 05:16
-
-
Save chrislewis/180141 to your computer and use it in GitHub Desktop.
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
class User(val firstName: String, val lastName: String) | |
trait UserDao { | |
def findByFirstName(firstName: String): User | |
} | |
object defaultUserDao extends UserDao { | |
override def findByFirstName(firstName: String) = new User(firstName, "Lewis") | |
} | |
object AppConfig { | |
var userDao: () => UserDao = () => defaultUserDao | |
} | |
trait UserServiceConfig { | |
val userDao: () => UserDao = () => AppConfig.userDao() | |
} | |
class UserService extends UserServiceConfig { | |
def findUser(firstName: String) = { | |
userDao().findByFirstName(firstName) | |
} | |
} | |
var us = new UserService | |
println(us.findUser("Chris").firstName) // "Chris" | |
AppConfig.userDao = () => new UserDao() { | |
override def findByFirstName(firstName: String) = new User("Mocked", "User") | |
} | |
println(us.findUser("Chris").firstName) // "Mocked" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment