Bridge
Category
Structural pattern
Problem
Decouple an abstraction from its implementation so that the two can vary independently. Useful when both the class and the possible implementations need to be extended at runtime.
Solution
- Create an abstraction hierarchy that delegates to an implementation hierarchy
- The abstraction holds a reference to the implementation interface, not a concrete class
- Changes to either hierarchy do not affect the other
Structure
- Abstraction: Defines the abstraction interface; holds a reference to the Implementor
- RefinedAbstraction: Extends the Abstraction interface
- Implementor: Defines the interface for implementation classes
- ConcreteImplementor: Implements the Implementor interface
- Client: Uses the Abstraction through the Implementor
Key Benefits
- Separation of concerns — Abstraction and implementation evolve independently
- Reduced class explosion — Avoids N x M class combinations from inheritance
- Runtime switching — Implementation can be changed at runtime
Trade-offs
- Adds an abstraction layer that increases complexity
- Requires careful design to ensure the delegation works correctly
When to Use
- You want to avoid a permanent binding between an abstraction and its implementation
- Both the abstraction and its implementations should be extensible by subclassing
- Changes in the implementation should not affect clients
- You want to hide the implementation from clients