Skip to content

Commit 114e189

Browse files
committed
Add the bridge design pattern in Kotlin
1 parent 22a1f54 commit 114e189

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
interface Color {
2+
fun getColor()
3+
}
4+
5+
class Yellow: Color {
6+
override fun getColor() {
7+
println("Yellow")
8+
}
9+
}
10+
11+
class Red: Color {
12+
override fun getColor() {
13+
println("Red")
14+
}
15+
}
16+
17+
interface House {
18+
val color: Color
19+
fun show()
20+
}
21+
22+
class WoodHouse(override val color: Color): House {
23+
override fun show() {
24+
print("The wood house color is ")
25+
color.getColor()
26+
}
27+
}
28+
29+
class RockHouse(override val color: Color): House {
30+
override fun show() {
31+
print("The rock house color is ")
32+
color.getColor()
33+
}
34+
}
35+
36+
fun main() {
37+
val yellowWoodHouse = WoodHouse(color = Yellow())
38+
yellowWoodHouse.show()
39+
val yellowRockHouse = RockHouse(color = Yellow())
40+
yellowRockHouse.show()
41+
val redWoodHouse = WoodHouse(color = Red())
42+
redWoodHouse.show()
43+
val redRockHouse = RockHouse(color = Red())
44+
redRockHouse.show()
45+
}

0 commit comments

Comments
 (0)