Design pattern "Bridge"
Real world example
Letβs consider a scenario where youβre building a web application that has different types of UI components like Buttons
, Panels
, and Modals
. Each of these components can have different themes, like DarkTheme
, LightTheme
, or CustomTheme
.
Without the Bridge pattern, you might end up creating separate classes for each combination like DarkThemeButton
, LightThemeButton
, DarkThemePanel
, LightThemePanel
, and so on. This approach quickly becomes unmanageable as the number of components and themes grow.
With the Bridge pattern, you can separate the abstraction (UI components) from the implementation (themes).
Before
Loading graph...
After
Loading graph...
In plain words
Bridge pattern is about preferring composition over inheritance. Implementation details are pushed from a hierarchy to another object with a separate hierarchy.
Wikipedia definition
The bridge pattern is a design pattern used in software engineering that is meant to βdecouple an abstraction from its implementation so that the two can vary independentlyβ
Programmatic example
Easy example diagram
Here is an another example of the bridge design pattern. Instead of having to manage a multitude of classes like BlueRectangle
, RedRectangle
, BlueCircle
, RedCircle
, we will prefer separate the classes and prioritize composition over inheritance.
Before
Loading graph...
After
Loading graph...
Some other examples
Here are some scenarios where the Bridge design pattern would be a good fit:
- Graphics: Different graphic rendering engines (OpenGL, DirectX) can be switched at runtime, but the high-level drawing functions remain the same.
- Databases: Different databases (MySQL, PostgreSQL, SQLite) can be used interchangeably without changing the way queries are executed.
- User Interfaces: Different user interface libraries (Qt, GTK) can be used interchangeably without changing the high-level UI manipulation functions.
- Operating Systems: Different operating systems (Windows, Linux, MacOS) can be used interchangeably without changing the high-level system calls.
- Payment Gateways: Different payment gateways (PayPal, Stripe, Square) can be used interchangeably without changing the high-level payment processing functions.
- Messaging Systems: Different messaging systems (RabbitMQ, Kafka, ActiveMQ) can be used interchangeably without changing the high-level message sending and receiving functions.
- Cloud Providers: Different cloud providers (AWS, Google Cloud, Azure) can be used interchangeably without changing the high-level resource management functions.
Design pattern "Strategy"
Let's learn what is the "Strategy" design pattern
Design pattern "Visitor"
Let's learn what is the "Visitor" design pattern π€ππΆπ§³
Design pattern "Iterator"
Let's learn what is the "Iterator" design pattern π
Design pattern "Observer"
Let's learn what is the "Observer" design pattern ππππ
Design pattern "Memento"
Let's learn what is the "Memento" design pattern πΈοΈπΌοΈπΌοΈππ°οΈπ
Design pattern "Facade"
Let's learn what is the "Facade" design pattern πΉοΈποΈπ¨
Design pattern "Decorator"
Let's learn what is the "Decorator" design pattern πͺβββ
Design pattern "Composite"
Let's learn what is the "Composite" design pattern π³πΏπΏ