Bridge: two axes that change independently
- Lesson 12 of 32
- 10 min
- Intermediate
- 12 steps
Notifications sent by email, SMS and push, with one subclass per combination. You will see why that hierarchy explodes and how two hierarchies joined by a field grow by adding.
- Concrete classes
- 6
- request
- response
Step 1 of 12
Problem
One subclass for every combination
Your app sends two notifications: the password reset and the weekly digest. And it sends them over two channels: email and SMS.
The first idea is usually inheritance: PasswordReset and WeeklyDigest extend Notification, and each one gets a subclass per channel: ResetEmail, ResetSms, DigestEmail and DigestSms. That is 4 concrete classes for 2 × 2 combinations.
Step 2 of 12
Problem
A new channel multiplies the classes
Product asks for push notifications. One class is not enough: you need ResetPush and DigestPush, one for every notification that already exists.
Now there are 6 classes for 2 × 3 combinations, and the sendPush call is copied in two places. If the way you send push changes, you change two classes.
What changed
- ResetPush
- DigestPush
Step 3 of 12
Problem
The hierarchy grows like a table
The count is messages × channels. If a third notification arrives tomorrow, the invoice, that is 3 × 3 = 9 classes. With a fourth channel, 3 × 4 = 12.
Every new axis value multiplies the work on the other axis, because inheritance mixes two different questions into one tree.
Step 4 of 12
Concept
Two questions that do not depend on each other
Look at the names and two questions show up: what is said (reset the password, the digest) and how it is sent (email, SMS, push).
Each question is an axis of change. A new message has nothing to do with a new channel. If each axis lives in its own tree, each change touches only its own side.
Step 5 of 12
Solution
Bridge: a reference instead of inheritance
Split the axes into two hierarchies. On one side, the abstraction: Notification and its messages, PasswordReset and WeeklyDigest. On the other, the implementation: the Channel interface, with EmailChannel, SmsChannel and PushChannel.
The bridge is a field: Notification has a Channel and delegates the sending to it. You no longer inherit from a channel: you receive one. That is 5 concrete classes, 2 + 3, for the same 6 combinations.
What changed
- interface Channel
- Notification receives a Channel
- 6 subclasses, one per combination
Step 6 of 12
Technique
The combination is picked when you create the object
The combination no longer lives in a class name: new PasswordReset(new SmsChannel()) is a reset by SMS, and with new EmailChannel() it is the same message by email.
When you call send, PasswordReset builds the text and calls this.channel.deliver(...). It does not know which channel is behind it: it only knows the Channel interface. That is why you can pick the channel at runtime, for example from the user's preference.
Step 7 of 12
Solution
The invoice costs one class
The invoice arrives. With inheritance it meant 3 new classes, one per channel. With Bridge it is one: Invoice extends Notification, and it works with all three channels from day one.
Total: 3 + 3 = 6 classes instead of 3 × 3 = 9. A fourth channel would also cost a single class: 3 + 4 = 7 instead of 12. Each axis grows by adding, not multiplying.
What changed
- Invoice
Step 8 of 12
Concept
Where you meet it: React renderers
You can read React renderers as a Bridge. Your components are the abstraction side: they describe what to show. Each renderer hands react-reconciler a host config: how to create a node and how to append a child on its platform.
That is how the same component model runs in the browser with React DOM and on mobile with React Native. React's docs do not call it Bridge: it is a way of reading it that helps you recognise the idea.
Step 9 of 12
Concept
Another case: JDBC drivers
In Java, your code uses the java.sql interfaces and does not know the database's concrete class. Each database brings its own Driver, and DriverManager asks them one by one whether they accept the connection URL.
You can read it as a Bridge too: your data access code is one side and the drivers are the other, joined by the Driver interface. The JDBC docs do not use the word Bridge either.
Step 10 of 12
Trade-off
Bridge is not Adapter
They look alike in a diagram: one class talks to another through an interface. The difference is when and what for.
Adapter is added afterwards, to make an existing class with a different interface fit. Bridge is designed from the start, so two sides can grow separately and be combined.
And not every interface is a bridge: it only is when there are two real axes that change. In TypeScript, the implementation side is often just an interface of functions, with no class tree.
Step 11 of 12
Trade-off
When not to use Bridge
If only one axis varies, there is no bridge to build. A single message sent over several channels is one class that receives a Channel: that is Strategy or plain dependency injection, without the Notification hierarchy.
It does not pay off either when the combinations are few and stable: two concrete classes read faster than two hierarchies and an interface. And picking the wrong axes is expensive to undo, because the rest of the code leans on that split.
Step 12 of 12
Summary
Summary
When a hierarchy grows as m × n, it is mixing two axes of change.
Bridge splits them: an abstraction with its own inheritance and an implementation behind an interface.
The bridge is a field: the abstraction has an implementation and delegates the work to it.
The axes grow by adding (3 + 3 = 6 classes instead of 9) and are combined when you create the object.
It is not Adapter, and it is overkill when only one axis varies.
← → step · R replay · Space pause
You will learn
- Spot a hierarchy that grows as **messages × channels**. (covered)
- Split the two axes of change into two hierarchies. (covered)
- Join them with a field: the abstraction **has** an implementation. (covered)
- Recognise the idea in React renderers and JDBC drivers. (covered)
- Tell Bridge from Adapter and know when it is overkill. (covered)