Quick revision
The short answer
The module has one class for every pair of message type and channel, so three message types and three channels already need nine classes, and each new channel adds one class per message type. The bridge pattern splits this into two small hierarchies: messages (OrderMessage, AlertMessage, ReminderMessage) that decide what to say, and channels (EmailChannel, SmsChannel, PushChannel) that decide how to deliver it. Each message holds a channel and delegates the sending to it, so we need n plus m classes instead of n times m.
What to remember
In this answer, we saw that classes like OrderSms and AlertPush combine two independent choices, what the message says and how it travels, which makes the class count n times m and copies delivery code everywhere. The bridge pattern separates a message abstraction from a channel interface, lets each message hold a channel and delegate the sending, and so brings the count down to n plus m. New channels and new message types are then added independently, and composition replaces inheritance on the channel axis.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The module has one class for every pair of message type and channel, so three message types and three channels already need nine classes, and each new channel adds one class per message type. The bridge pattern splits this into two small hierarchies: messages (OrderMessage, AlertMessage, ReminderMessage) that decide what to say, and channels (EmailChannel, SmsChannel, PushChannel) that decide how to deliver it. Each message holds a channel and delegates the sending to it, so we need n plus m classes instead of n times m.
Why this question is asked
Class explosions like this one grow quietly. Nobody designs nine classes on purpose; someone adds OrderSms next to OrderEmail, then AlertPush next to AlertEmail, and two years later adding WhatsApp means writing five new classes that each copy the same delivery code. Interviewers ask this refactoring because it tests whether we can see the two independent directions of change hidden inside one inheritance tree, and whether we know when composition beats inheritance. In this answer, we will look at why the current design multiplies classes, find the two axes inside it, refactor it step by step into a bridge, wire it in Spring, and check what the new design costs us.
1Why the Current Hierarchy Multiplies
Look at the class names: OrderEmail, OrderSms, OrderPush, AlertEmail, AlertSms, AlertPush, ReminderEmail. Every name is made of two words. The first word says what the message is about, which decides its content: an order confirmation, a security alert, a payment reminder. The second word says how it travels, which decides the delivery mechanics: SMTP, an SMS gateway, a push service such as Firebase.
Inheritance can only express one of those choices at a time. A class extends one parent, so the designers picked a parent per message type or per channel and then copied the other half of the behaviour into each subclass. That is why the count is the product of the two lists. With 3 message types and 3 channels we have 9 classes. Adding a fourth channel adds 3 more classes, and adding a fourth message type adds 4 more. The delivery code for SMS, such as phone formatting and the 160 character limit, is repeated in OrderSms, AlertSms and ReminderSms, so a bug fix must be made three times.
Notice that the two halves of each class change for different reasons. Marketing changes the wording of an order confirmation; the platform team changes the SMS provider. When two reasons to change live in one class, every change touches more code than it should.
2Finding the Two Axes
The bridge pattern names these two directions. The abstraction is the part the rest of the application talks to: here, a message that knows its own content. The implementation, sometimes called the implementor, is the low-level work the abstraction relies on: here, a channel that can deliver a piece of text to a recipient. The bridge is simply the reference from the abstraction to the implementation, a field holding a channel.
So we split the tree in two. On one side, a small abstract class Notification with subclasses OrderMessage, AlertMessage and ReminderMessage. On the other side, an interface Channel with EmailChannel, SmsChannel and PushChannel. Neither side extends the other, and each grows on its own.
| Situation | Combination classes | Bridge classes |
|---|---|---|
| 3 messages, 3 channels | 9 | 3 + 3 = 6 |
Add WhatsApp channel | 12 (3 new) | 7 (1 new) |
| Add Invoice message | 16 (4 new) | 8 (1 new) |
| 6 messages, 5 channels | 30 | 11 |
3Refactoring Into a Bridge
We start with the implementation side, because it is the simplest. A channel needs to know nothing about orders or alerts. It receives a recipient and a rendered message and delivers it.
6 messages in order. Play them, or step through them one at a time.
The key line is sendTo(): the message renders its content and hands it to whatever channel it was built with. The message does not know whether it travels by SMS or email, and the channel does not know whether it is carrying an order or an alert. That is the composition replacing inheritance on one axis: the channel axis is now chosen by passing an object, not by picking a subclass.
There is a catch worth handling at this point. Channels differ in what they can show: an email has a subject and HTML, an SMS has 160 characters, a push notification has a title and a short body. The Rendered record must be general enough for all of them, and each channel takes what it needs from it. If one channel needs something truly special, we add an optional field rather than letting messages check which channel they are using, because that check would couple the two sides again.
4Wiring It in Spring
In a Spring application each channel becomes a singleton bean, because channels are stateless and expensive things like SMTP sessions or HTTP clients live inside them. The messages are not beans; they are small objects created per event. A service picks the channel from the customer's preference and builds the message.
Adding WhatsApp is now one class, WhatsAppChannel, registered as a bean. Every existing message can use it the same day, without being touched. Adding an invoice message is one class that works with all channels at once.
5What the Bridge Costs
The refactoring is not free. There is one more level of indirection, so a reader following a notification must look in two places. The Channel interface must suit every channel, and getting that shared contract right is the real design work. And if the module only ever had one channel, the split would be pure overhead. So the pattern pays off exactly when both axes really vary, as they do here, where the class list already shows three of each.
To migrate safely, we write tests that capture what each old class sends, for example by recording calls to a fake gateway, then replace the old classes one combination at a time and run the same tests against the new pair.
6Follow-up questions
How is this different from the strategy pattern?
The code looks similar, because in both cases an object holds an interface and delegates to it. The difference is intent and scope. A strategy swaps one algorithm inside a single class. A bridge separates two whole hierarchies that both grow over time, here message types and channels, so that either side can get new members without touching the other.
Could we use generics or a switch on the channel instead?
A switch inside each message would remove the extra classes, but every message would need to change when a channel is added, which is the problem we started with. Generics do not help because the variation is behaviour, not type. Composition through an interface is what lets each side change independently.
Where does the choice of channel belong?
Outside both hierarchies, in the code that knows the customer's preferences or the event's urgency, such as the NotificationService above. Keeping that decision out of the message classes keeps them reusable, and makes it easy to send one message on two channels, for example a security alert by both push and email.
7Conclusion
In this answer, we saw that classes like OrderSms and AlertPush combine two independent choices, what the message says and how it travels, which makes the class count n times m and copies delivery code everywhere. The bridge pattern separates a message abstraction from a channel interface, lets each message hold a channel and delegate the sending, and so brings the count down to n plus m. New channels and new message types are then added independently, and composition replaces inheritance on the channel axis.
Open with
The problem with this module is that every class name is really two decisions: what the message is about and which channel it goes through. Because inheritance can only express one of those, we ended up with one class per combination, so three message types and three channels give nine classes, and the SMS sending code is copied into every SMS class.
Then, if they want more
I would refactor it with the bridge pattern. On one side I would have a Notification abstraction with OrderMessage, AlertMessage and ReminderMessage, which only know how to render their content. On the other side a Channel interface with EmailChannel, SmsChannel and PushChannel, which only know how to deliver a rendered message to a customer.
Each Notification gets a Channel in its constructor and its send method just renders and delegates to that channel. So composition replaces inheritance on the channel axis. In Spring the channels are stateless singleton beans and a notification service chooses the channel from the customer's preference.
Now adding WhatsApp is one new class that all messages can use, and adding an invoice message is one class that works on every channel, so we go from n times m classes to n plus m.
The main design effort is the shared Channel contract, so I would make the rendered message general enough for all channels and keep channel checks out of the messages. I would migrate combination by combination, with tests that record what each old class sent.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A notifications module has classes OrderEmail, OrderSms, OrderPush, AlertEmail...": https://scrivio-interview-library.netlify.app/java/structural-patterns/51cbf72765/