Bridge
Separates abstraction from implementation so both can vary independently.
Understanding Bridge
The Bridge pattern decouples an abstraction from its implementation so that the two can evolve independently. Instead of creating a class hierarchy where abstractions and implementations are tightly bound, you create two separate hierarchies — one for the high-level control logic and one for the low-level platform-specific work — connected by composition (the "bridge"). In Go, the abstraction struct holds an interface field pointing to the implementation, enabling any combination of abstraction and implementation at runtime.
Key Concepts
- •Abstraction — the high-level layer that defines operations in terms of the implementation interface (e.g., RemoteControl)
- •Implementation interface — defines the low-level operations that concrete implementations provide (e.g., Device)
- •Composition over inheritance — the abstraction holds a reference to the implementation rather than inheriting from it
- •Independent variation — you can add new abstractions (AdvancedRemote) and new implementations (SmartTV) without modifying existing code
When to Use
- • Both the abstraction and implementation need to be extended independently
- • You want to avoid a cartesian product of types (M abstractions × N implementations)
- • You need to switch implementations at runtime
- • Cross-platform code where platform details vary but high-level logic stays the same
- • You only have one implementation — an interface alone is enough
- • The abstraction and implementation rarely change independently
- • Added indirection makes the code harder to follow than the problem it solves
Structure
How It Works
Abstraction Layer
Define high-level operations independent of implementation.
Basic Implementation
Remote control (abstraction) working with different devices (implementations):
Real-World Example: Graphics Rendering
Shapes that can be rendered with different rendering engines: