Bridge Design Pattern in Java
Separate an abstraction from its implementation so both can change independently.
What is Bridge?
Bridge uses composition instead of inheritance. The high-level object keeps a reference to an implementation object.
Beginner-friendly Java example
The following example shows the central idea of this pattern in a small, practical scenario.
interface Device {
void turnOn();
}
class Television implements Device {
public void turnOn() {
System.out.println("TV on");
}
}
class Remote {
private final Device device;
Remote(Device device) {
this.device = device;
}
void pressPower() {
device.turnOn();
}
}Benefits and trade-offs
Benefits
- Separates responsibilities clearly.
- Improves flexibility as the application grows.
- Encapsulates structural complexity.
Trade-offs
- Can introduce extra wrapper classes.
- Choose the pattern only when the complexity is real.
When should you use it?
- Supporting multiple platforms
- Separating controls from devices
- Avoiding a large inheritance tree
Key takeawaySeparate an abstraction from its implementation so both can change independently. Start with the smallest structure that solves the coupling or composition problem.
