Created
March 22, 2011 19:07
-
-
Save upeter/881826 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
package snippets | |
object DIExamples { | |
/* | |
* http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di.html | |
* http://debasishg.blogspot.com/2011/03/pushing-envelope-on-oo-and-functional.html | |
* http://www.assembla.com/wiki/show/liftweb/Dependency_Injection | |
*/ | |
case class Account(id:String, balance:Double) | |
def ??? = throw new Error("not implemented yet") | |
trait Dao[T] { | |
def findById(id:String):T | |
def save(t:T) | |
} | |
trait AccountService { | |
def findAccountById(id:String):Account | |
/** | |
* Check for sufficient balance and perform transfer | |
*/ | |
def transfer(from:Account, to:Account, amount:Double):(Account, Account) | |
} | |
class Client { | |
val accountService:AccountService = ??? | |
def performTransfer() = { | |
val a1 = accountService.findAccountById("1.A") | |
val a2 = accountService.findAccountById("2.B") | |
accountService.transfer(a1, a2, 1000) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Guys, take a look at http://skillsmatter.com/podcast/scala/how-we-mostly-moved-from-java-to-scala