forked from mistrydarshan99/Design-Patterns-In-Kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Composite.kt
41 lines (29 loc) · 918 Bytes
/
Composite.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
open class Equipment(
open val price: Int,
val name: String
)
open class Composite(name: String) : Equipment(0, name) {
private val equipments = ArrayList<Equipment>()
override val price: Int
get() = equipments.map { it -> it.price }.sum()
fun add(equipment: Equipment) =
apply { equipments.add(equipment) }
}
class PersonalComputer : Composite("PC")
class Processor : Equipment(1070, "Processor")
class HardDrive : Equipment(250, "Hard Drive")
class Memory : Equipment(280, "Memory")
class CompositeTest {
@Test
fun `Composite`() {
val pc = PersonalComputer()
.add(Processor())
.add(HardDrive())
.add(Memory())
println(pc.price)
assertThat(pc.name).isEqualTo("PC")
assertThat(pc.price).isEqualTo(1600)
}
}