Unlike some other design patterns we've met, Bridge is less about a smart way to compose objects, and more about guidelines on how not to abuse inheritance. The way it works is actually very simple.
Let's go back to the strategy game we're building. We have an interface for all our infantry units:
interface Infantry {
fun move(x: Long, y: Long)
fun attack(x: Long, y: Long)
}
We have the concrete implementations:
open class Rifleman : Infantry {
override fun attack(x: Long, y: Long) {
// Shoot
}
override fun move(x: Long, y: Long) {
// Move at its own pace
}
}
open class Grenadier : Infantry {
override fun attack(x: Long, y: Long) {
// Throw grenades
}
override fun move(x: Long, y: Long) {
// Moves slowly, grenades are heavy
}
}
What if we want to have the ability to upgrade our units?
Upgraded units should...