GoF pattern • Structural
GoFStructuralJava example
Bridge solves a specific structural problem in the design.
Separate abstraction from implementation so both can vary independently.
Pattern overview
GoF pattern
Separate abstraction from implementation so both can vary independently.
Intent
Decouple an abstraction from its implementation so the two can evolve separately.
Problem
Inheritance would explode into many combinations when both high-level behavior and low-level implementation vary.
Solution
Split the abstraction and implementation into separate hierarchies connected by composition.
Fit and tradeoffs
FamilyGoF
CategoryStructural
Best used when
- When two dimensions of change vary independently.
- When you need to support multiple platforms or channels cleanly.
Tradeoffs
- Adds indirection that may feel abstract at first.
- Not necessary if only one dimension actually varies.
Java example
This example is intentionally compact so the pattern shape stays easy to see.
interface MessageSender {
void send(String subject, String body);
}
abstract class Notification {
protected final MessageSender sender;
protected Notification(MessageSender sender) {
this.sender = sender;
}
abstract void notifyUser(String body);
}
final class AlertNotification extends Notification {
AlertNotification(MessageSender sender) { super(sender); }
void notifyUser(String body) { sender.send("Alert", body); }
}