Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package test
- package view {
- // domain access interface for this view
- trait ViewDomainAccess {
- def doSomeDomainWork(s: String, i: Int): Unit
- }
- // view implementation
- trait View {
- this: ViewDomainAccess => // self type
- // main constructor
- doSomeDomainWork("foo", 23)
- doSomeDomainWork("bar", 42)
- }
- }
- package domain {
- // persistence interface for this domain
- trait DomainPersistence {
- def saveData(s: String): Boolean
- }
- // domain implementation
- trait DomainFunction extends view.ViewDomainAccess {
- this: DomainPersistence => // self type
- def doSomeDomainWork(s: String, i: Int) {
- println("DomainFunction.doSomeDomainWork(%s, %d) called".format(s, i))
- if(!saveData(s + " ==> " + i)) println("ERROR: can't save data")
- }
- }
- // another domain implementation
- trait OtherDomainFunction extends view.ViewDomainAccess {
- this: DomainPersistence => // self type
- def doSomeDomainWork(s: String, i: Int) {
- println("OtherDomainFunction.doSomeDomainWork(%s, %d) called" format(s, i))
- if(!saveData(" ...>> %s[%d]" format (s, i))) println("ERROR: can't save data")
- }
- }
- }
- package persistence {
- // persistence implementation
- trait Persistence extends domain.DomainPersistence {
- def saveData(s: String): Boolean = {
- println("save data: " + s)
- true // always OK
- }
- }
- }
- object DependencyInjection {
- def test() = {
- // import all needed implementations
- import view.View
- import domain.{DomainFunction, OtherDomainFunction}
- import persistence.Persistence
- // version 1
- val m1 = new View with
- DomainFunction with
- Persistence
- println("-" * 60)
- // version 2 with other domain function implementation
- val m2 = new View with
- OtherDomainFunction with
- Persistence
- }
- def main(args: Array[String]) = {
- test()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement