Bridge Pattern: Separating Abstraction from Implementation
What you'll learn: How the Bridge pattern decouples an abstraction from its implementation so both can evolve independently without affecting each other.
The Core Problem
Imagine you're building a graphics system with different shapes (circles, squares) that can be rendered on different platforms (Windows, Linux). Without the Bridge pattern, you'd create WindowsCircle, LinuxCircle, WindowsSquare, LinuxSquare—a combinatorial explosion! Every new shape or platform multiplies your classes.
How Bridge Works
The Bridge pattern splits your design into two independent hierarchies:
- Abstraction hierarchy: High-level logic (e.g., Shape classes)
- Implementation hierarchy: Platform-specific details (e.g., Renderer classes)
The abstraction holds a reference to an implementation object and delegates actual work to it. This composition-over-inheritance approach means shapes don't know how rendering happens—they just ask their renderer to do it.
The Mental Model
Think of a universal remote control (abstraction) and different devices (implementations). The remote has high-level buttons like "power" and "volume," but the actual electrical signals vary by device. You can add new remotes or new devices independently—the remote doesn't hardcode Sony-specific logic; it just tells its device object what to do.
When to Use It
- You have two dimensions of variation that multiply (shapes × platforms, file formats × compression algorithms)
- You want to avoid permanent binding between abstraction and implementation
- Both abstractions and implementations should be extensible through subclassing
- Changes in implementation shouldn't require recompiling abstraction code
Key Takeaway: Bridge uses composition to separate what something is (abstraction) from how it does it (implementation), letting both hierarchies grow independently without creating a class explosion.