-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCakePattern.scala
96 lines (63 loc) · 1.71 KB
/
CakePattern.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package app.impl.patterns.di
import org.junit.Test
/**
* Created by pabloperezgarcia on 26/10/2017.
*/
class CakePattern {
trait SimpleTrait {
def addOne(x: Int): Int = {
x + 1
}
}
class SimpleClass extends SimpleTrait
val addOne = new SimpleClass
trait UserRepositoryComponent {
def userLocator: UserLocator
def userUpdater: UserUpdater
trait UserLocator {
def findAll: List[String]
}
trait UserUpdater {
def save(user: String): Unit
}
}
trait UserRepositoryJPAComponent extends UserRepositoryComponent {
def userLocator = new UserLocatorJPA()
def userUpdater = new UserUpdaterJPA()
class UserLocatorJPA() extends UserLocator {
def findAll = List("Paul")
}
class UserUpdaterJPA() extends UserUpdater {
def save(user: String) {
println(user)
}
}
}
trait UserServiceComponent {
def userService: UserService
trait UserService {
def findAll: List[String]
def save(user: String)
}
}
trait DefaultUserServiceComponent extends UserServiceComponent {
this: UserRepositoryComponent =>
def userService = new DefaultUserService
class DefaultUserService extends UserService {
def findAll = userLocator.findAll
def save(user: String) {
userUpdater.save(user: String)
}
}
}
object ApplicationLive {
val userServiceComponent = new DefaultUserServiceComponent with UserRepositoryJPAComponent
val userService = userServiceComponent.userService
}
@Test
def mainTest(): Unit = {
val userService = ApplicationLive.userService
userService.findAll.foreach(value => println(value))
userService.save("George")
}
}