Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.96 KB | None | 0 0
  1. package test
  2.  
  3. package view {
  4.   // domain access interface for this view
  5.   trait ViewDomainAccess {
  6.     def doSomeDomainWork(s: String, i: Int): Unit
  7.   }
  8.  
  9.   // view implementation
  10.   trait View {
  11.     this: ViewDomainAccess =>  // self type
  12.  
  13.     // main constructor
  14.     doSomeDomainWork("foo", 23)
  15.     doSomeDomainWork("bar", 42)
  16.   }
  17. }
  18.  
  19. package domain {
  20.   // persistence interface for this domain
  21.   trait DomainPersistence {
  22.     def saveData(s: String): Boolean
  23.   }
  24.  
  25.   // domain implementation
  26.   trait DomainFunction extends view.ViewDomainAccess {
  27.     this: DomainPersistence =>  // self type
  28.  
  29.     def doSomeDomainWork(s: String, i: Int) {
  30.       println("DomainFunction.doSomeDomainWork(%s, %d) called".format(s, i))
  31.       if(!saveData(s + " ==> " + i)) println("ERROR: can't save data")
  32.     }
  33.   }
  34.  
  35.   // another domain implementation
  36.   trait OtherDomainFunction extends view.ViewDomainAccess {
  37.     this: DomainPersistence =>  // self type
  38.  
  39.     def doSomeDomainWork(s: String, i: Int) {
  40.       println("OtherDomainFunction.doSomeDomainWork(%s, %d) called" format(s, i))
  41.       if(!saveData(" ...>> %s[%d]" format (s, i))) println("ERROR: can't save data")
  42.     }
  43.   }
  44. }
  45.  
  46. package persistence {
  47.   // persistence implementation
  48.   trait Persistence extends domain.DomainPersistence {
  49.     def saveData(s: String): Boolean = {
  50.       println("save data: " + s)
  51.       true // always OK
  52.     }
  53.   }
  54. }
  55.  
  56. object DependencyInjection {
  57.  
  58.   def test() = {
  59.     // import all needed implementations
  60.     import view.View
  61.     import domain.{DomainFunction, OtherDomainFunction}
  62.     import persistence.Persistence
  63.  
  64.     // version 1
  65.     val m1 = new View with
  66.                  DomainFunction with
  67.                  Persistence
  68.  
  69.     println("-" * 60)
  70.  
  71.     // version 2 with other domain function implementation
  72.     val m2 = new View with
  73.                  OtherDomainFunction with
  74.                  Persistence
  75.   }
  76.  
  77.   def main(args: Array[String]) = {
  78.     test()
  79.   }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement