-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractFactory.kt
42 lines (36 loc) · 1.13 KB
/
AbstractFactory.kt
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
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
//Produto Abstrato
interface Simpson
//Produtos Concretos
class HomerSimpson : Simpson
//
class MargeSimpson : Simpson
//Fábrica Abstrata
abstract class SimpsonFactory {
abstract fun makeSimpson(): Simpson
companion object {
inline fun <reified T : Simpson> createFactory(): SimpsonFactory =
when (T::class) {
HomerSimpson::class -> HomerFactory()
MargeSimpson::class -> MargeFactory()
else -> throw IllegalArgumentException()
}
}
}
//Fábricas Concretas
class HomerFactory : SimpsonFactory() {
override fun makeSimpson(): Simpson = HomerSimpson()
}
class MargeFactory : SimpsonFactory() {
override fun makeSimpson(): Simpson = MargeSimpson()
}
class AbstractFactoryTest {
@Test
fun `Abstract Factory`() {
val simpsonFactory = SimpsonFactory.createFactory<MargeSimpson>()
val simpson = simpsonFactory.makeSimpson()
println("Simpson instanciado: $simpson")
assertThat(simpson).isInstanceOf(MargeSimpson::class.java)
}
}