Member-only story
Bridge design pattern
The Bridge design pattern is a structural pattern used to separate the abstraction (interface) from its implementation so that both can vary independently. It allows the two to evolve separately and makes it easier to extend and maintain large systems by decoupling abstractions and implementations.
In simpler terms, the Bridge pattern enables you to create two hierarchies: one for the abstraction (interface or high-level functionality) and another for the implementation (concrete implementation details). It helps in avoiding a direct coupling between them, allowing changes in one hierarchy to not affect the other.
Example:- Exploding Class Hierarchy in Code
protocol Car {
func drive()
}
class Sedan: Car {
func drive() {
print("drive a sedan")
}
}
class SUV: Car {
func drive() {
print("drive a SUV")
}
}Then what if we want to describe colored cars? This requirement add another variable in current structure. Normally, we need to add a inheritance level.
// explosion class hierarchy increase so on
class RedSedan: Sedan {
func drive() {
print("drive a red sedan")
}
}The structure is in the below figure






