0
0
LLDsystem_design~7 mins

Bridge pattern in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When a system has multiple dimensions of variation, combining them directly leads to a large number of classes that are hard to maintain and extend. For example, if you have different types of devices and different ways to control them, creating a class for every combination causes code duplication and rigidity.
Solution
The Bridge pattern separates the abstraction (interface) from its implementation so they can vary independently. It uses composition to link an abstraction object to an implementation object, allowing you to change either side without affecting the other. This reduces the number of classes and improves flexibility.
Architecture
Abstraction
(interface)
Implementation
RefinedAbst.
ConcreteImplB
ConcreteImplB

This diagram shows the abstraction and implementation as separate hierarchies connected by a bridge. The abstraction delegates work to the implementation, allowing both to evolve independently.

Trade-offs
✓ Pros
Reduces the number of classes by decoupling abstraction and implementation.
Improves code flexibility and extensibility by allowing independent changes.
Supports runtime switching of implementations without changing the abstraction.
✗ Cons
Introduces additional layers of indirection, which can add complexity.
Requires careful design to keep abstraction and implementation interfaces stable.
May increase the number of objects created, impacting memory usage.
Use when a system has multiple independent dimensions of variation that would otherwise cause combinatorial explosion of classes, especially when you expect these dimensions to change or extend over time.
Avoid when the abstraction and implementation are unlikely to change independently or when the system is simple with few variations, as the added complexity is unnecessary.
Real World Examples
Microsoft
Used in GUI frameworks to separate window abstractions from platform-specific implementations, allowing the same window interface to work on different operating systems.
IBM
Applied in printer drivers to separate the printer interface from the actual printer hardware implementations, enabling support for multiple printer models.
Google
Used in Android to separate UI components from rendering engines, allowing different rendering backends without changing UI logic.
Code Example
The before code tightly couples dialog classes with specific button implementations, causing class explosion. The after code separates the abstraction (Dialog) from the implementation (ButtonImplementation), allowing independent extension and reuse.
LLD
### Before Bridge Pattern (tight coupling, many classes)

class WindowsButton:
    def draw(self):
        print("Drawing button in Windows style")

class MacOSButton:
    def draw(self):
        print("Drawing button in MacOS style")

class WindowsDialog:
    def render(self):
        button = WindowsButton()
        button.draw()

class MacOSDialog:
    def render(self):
        button = MacOSButton()
        button.draw()

### After Bridge Pattern (decoupled abstraction and implementation)

class ButtonImplementation:
    def draw_button(self):
        pass

class WindowsButtonImpl(ButtonImplementation):
    def draw_button(self):
        print("Drawing button in Windows style")

class MacOSButtonImpl(ButtonImplementation):
    def draw_button(self):
        print("Drawing button in MacOS style")

class Dialog:
    def __init__(self, button_impl: ButtonImplementation):
        self.button_impl = button_impl

    def render(self):
        self.button_impl.draw_button()

# Usage
windows_dialog = Dialog(WindowsButtonImpl())
windows_dialog.render()  # Drawing button in Windows style

mac_dialog = Dialog(MacOSButtonImpl())
mac_dialog.render()      # Drawing button in MacOS style
OutputSuccess
Alternatives
Adapter pattern
Adapter changes the interface of an existing class to match what a client expects, usually for legacy or third-party code integration.
Use when: Choose Adapter when you need to make incompatible interfaces work together without changing their code.
Decorator pattern
Decorator adds responsibilities to objects dynamically without changing their interface, focusing on behavior extension rather than abstraction separation.
Use when: Choose Decorator when you want to add features to objects at runtime without affecting other objects.
Summary
Bridge pattern separates abstraction from implementation to reduce class explosion and improve flexibility.
It uses composition to connect abstraction and implementation hierarchies that can evolve independently.
This pattern is useful when multiple independent variations exist and need to be combined cleanly.