Structural 6 — Bridge
In one line
Decouple an abstraction from its implementation so both can vary independently — avoids the 2D class explosion.
Think of it like this
A TV remote and TVs. Any remote (basic or smart) can work with any TV brand (Sony or LG). You can make a new remote without making new TVs, and vice versa.
Key ideas
- 01
Solves: shapes × rendering engines (Circle×Raster, Circle×Vector, Square×Raster...) — the matrix problem.
- 02
Abstraction holds a reference to an Implementation (renderer), instead of inheriting it.
- 03
Similar to Strategy in shape; Bridge is more structural, Strategy behavioral.
Java / Spring map
- →
JDBC (the DriverManager/Connection API is the abstraction, vendor drivers are the implementations); SLF4J over Logback/Log4j.
Code & diagrams
Message urgency × delivery channel: M + N classes instead of M × N.
// Implementation hierarchy: HOW it is delivered
public interface Channel { void deliver(String to, String body); }
public final class EmailChannel implements Channel { public void deliver(String to, String b) { /* SMTP */ } }
public final class SmsChannel implements Channel { public void deliver(String to, String b) { /* SMS */ } }
// Abstraction hierarchy: WHAT kind of message, holding a bridge to a Channel
public abstract class Notification {
protected final Channel channel; // the bridge
protected Notification(Channel c) { this.channel = c; }
public abstract void send(String to, String msg);
}
public final class NormalNotification extends Notification {
public NormalNotification(Channel c) { super(c); }
public void send(String to, String msg) { channel.deliver(to, msg); }
}
public final class UrgentNotification extends Notification {
public UrgentNotification(Channel c) { super(c); }
public void send(String to, String msg) {
for (int i = 0; i < 3; i++) channel.deliver(to, "[URGENT] " + msg); // escalation policy
}
}
// 2 kinds × 2 channels = 4 combos from 4 classes; adding Push adds ONE class.
// new UrgentNotification(new SmsChannel()).send("+91...", "server down");Explain without notes
Bridge vs Adapter vs Strategy in one breath.
Practice
Model Notification (email/SMS) × urgency (normal/urgent) with Bridge.
Trade-offs
- ↔
Only pay for Bridge when BOTH dimensions actually vary.
Completion checklist
I can spot an M × N class explosion and refactor it into two hierarchies joined by a bridge.