-
Notifications
You must be signed in to change notification settings - Fork 696
/
FactoryMethod.kt
41 lines (32 loc) · 1.28 KB
/
FactoryMethod.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
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
sealed class Country {
object USA : Country() //Kotlin 1.0 could only be an inner class or object
}
object Spain : Country() //Kotlin 1.1 declared as top level class/object in the same file
class Greece(val someProperty: String) : Country()
data class Canada(val someProperty: String) : Country() //Kotlin 1.1 data class extends other class
//object Poland : Country()
class Currency(
val code: String
)
object CurrencyFactory {
fun currencyForCountry(country: Country): Currency =
when (country) {
is Greece -> Currency("EUR")
is Spain -> Currency("EUR")
is Country.USA -> Currency("USD")
is Canada -> Currency("CAD")
} //try to add a new country Poland, it won't even compile without adding new branch to 'when'
}
class FactoryMethodTest {
@Test
fun FactoryMethod() {
val greeceCurrency = CurrencyFactory.currencyForCountry(Greece("")).code
println("Greece currency: $greeceCurrency")
val usaCurrency = CurrencyFactory.currencyForCountry(Country.USA).code
println("USA currency: $usaCurrency")
assertThat(greeceCurrency).isEqualTo("EUR")
assertThat(usaCurrency).isEqualTo("USD")
}
}