Bridge
Last updated
Last updated
Bridge is a structural design pattern that lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
The Bridge design pattern is a structural pattern that decouples an abstraction from its implementation so that the two can vary independently.
In simpler terms, it helps in separating an abstraction's interface from its implementation, allowing them to change and evolve independently.
In this example:
The Shape
class represents the abstraction, and Circle
and Square
are concrete abstractions that extend Shape
.
They provide the interface for drawing shapes.
The Color
class represents the implementor, and RedColor
and BlueColor
are concrete implementors that extend Color
.
They provide the implementation for coloring shapes.
By separating shapes (abstraction) from their colors (implementation), we can easily create different colored shapes without modifying the shape classes.
This separation allows you to easily add new shapes or new colors without affecting existing code, promoting flexibility and maintainability.