Bridge
The Bridge design pattern is a great tool for avoiding the pitfalls of overusing inheritance and creating more flexible, maintainable code. It helps in separating abstractions from their implementations, allowing for better extensibility.
Let’s imagine we want to build a system to manage different kinds of troopers for the Galactic Empire.
We’ll begin by defining an interface to represent the basic behavior of a trooper:
interface Trooper {
fun move(x: Long, y: Long)
fun attackRebel(x: Long, y: Long)
}
With this interface, we establish the fundamental behavior that all troopers should have. Next, we’ll move on to implementing different types of troopers:
open class StormTrooper : Trooper {
override fun move(x: Long, y: Long) {
// Move at normal speed
}
override fun attackRebel(x: Long, y: Long) {
// Missed most of the time
}
}
open class ShockTrooper : Trooper {
override fun move(x: Long,...