Bridge
- Intent
- Split an idea into two parts that can change separately (what gets done and how it's done), and join them with a reference instead of inheritance.
- Application
- The box sends class and payment reminders by email, SMS or push. Separate what's said from how it's delivered, so each side grows on its own without one class per combination.
Four classes in a grid, one per combination: EmailClassReminder, SmsClassReminder, EmailPaymentReminder and SmsPaymentReminder, all highlighted.
SmsPaymentReminder is highlighted in red: its copy of the SMS text doesn't cut at 160 characters.
Reminder appears, highlighted, with ClassReminder and PaymentReminder; Reminder calls channel.deliver(…) across the bridge.
The Channel interface appears, highlighted, with EmailChannel, SmsChannel and PushChannel implementing it.
ClassReminder and PaymentReminder, highlighted, extend Reminder.
SmsChannel, highlighted, applies its 160-character limit to any reminder.
ClassReminder, PaymentReminder and SmsChannel, highlighted: both reminders go out by SMS across the bridge.
POST /api/reminders travels from the front desk to the controller and remind("leo", "class") to the service, which creates a ClassReminder with the SMS channel; ReminderService and SmsChannel are highlighted.
RestClient, highlighted, creates its requests with ClientHttpRequestFactory, also highlighted, which can be the JDK's or HttpURLConnection's.
Final view: the front desk, the controller, ReminderService, both reminders with Reminder and the three channels with Channel, joined by the bridge, all inside the Spring context.
Step 1 of 10
One class per combination
The box sends two kinds of reminder, for tomorrow's class and for the membership payment, and sends them by email or SMS. The first version has one class for each combination: EmailClassReminder, SmsClassReminder, EmailPaymentReminder and SmsPaymentReminder.
Now push arrives. That's two more classes. And a third kind of reminder would be three more. The classes grow like a table: kinds × channels.
Step 2 of 10
Copies that drift apart
Every SMS class repeats the same channel rules, like the 160-character limit. SmsPaymentReminder was copied from SmsClassReminder… without the cut. A payment reminder with a long note goes out whole and the carrier splits it into two messages. The OneClassPerCombinationTest test proves it.
Step 3 of 10
What's said
Bridge splits the classes into two sides. The first is the abstraction, Reminder: it decides the message's title and body.
It doesn't extend any channel: it has one (channel) and, when sending, hands it the title and body. That reference is the "bridge".
Step 4 of 10
How it's delivered
The other side is the implementation, Channel: it takes a title and a body and delivers them to a member. EmailChannel, SmsChannel and PushChannel know which address to send to and how to format it.
No channel knows what a class or a payment is.
Step 5 of 10
Each reminder, its text
ClassReminder and PaymentReminder are the refined abstractions: they only write their title and body ("Class tomorrow", "Hyrox prep at 18:00…"). They don't know which channel they'll go out on.
Step 6 of 10
Each rule, in its place
The 160-character limit is the SMS's business, so it lives in SmsChannel, once. It applies to the class reminder, the payment one with a long note and any that comes. It counts real characters (code points), so it never splits an emoji in half. Push cuts the title at 40 and the body at 120; email cuts nothing.
(160 is a simplification: a real SMS counts GSM-7 septets, € costs two, and any character outside GSM-7 drops the limit to 70.)
Step 7 of 10
Any combination
With two kinds of reminder and three channels there are six combinations, and not a single class per combination: new ClassReminder(sms, …) or new PaymentReminder(sms, …). A test walks all six.
Step 8 of 10
Each member, their channel
In Spring, each channel is a bean, and ReminderService gets the list of every Channel and indexes them by their name() ("email", "sms", "push"). Each member has a preferred channel: Ana email, Leo SMS, Eva push.
POST /api/reminders with Leo and "class": the service creates a ClassReminder with Leo's channel and sends it to +34600000002. With "channel": "email" in the request, the same reminder goes out by email; with no channel, or a blank one, the preferred one is used.
Step 9 of 10
Spring already does it for you
With RestClient you always write the same thing: get().uri(…).retrieve(). Underneath, a ClientHttpRequestFactory decides which HTTP client does the work: the java.net.http one (JdkClientHttpRequestFactory), the classic HttpURLConnection (SimpleClientHttpRequestFactory), Apache, Jetty or Reactor.
The test makes the same call with two different transports against its own server and gets the same answer. And the RestClient.Builder Spring Boot gives you picks one by itself from the classpath (Apache, Jetty, Reactor Netty, the JDK's and, as a last resort, HttpURLConnection).
It's a "degenerate" bridge: the abstraction side has a single class (RestClient has no refined subtypes), and the implementation is a request factory.
Step 10 of 10
Recap
You started with one class per combination and ended up with:
- An abstraction,
Reminder, that has a channel instead of inheriting it. - An implementation,
Channel, with one class per channel and its rules inside. - Free combinations: kinds of reminder and channels grow separately.
Don't mix it up with its neighbours: an Adapter fits something that already exists and didn't fit; a Bridge is designed from the start so two sides can change separately. With Strategy the shape is almost the same, and here Channel looks like one: a single method, injected. What makes it a Bridge is the intent: both sides are hierarchies meant to grow separately. In the book, besides, the implementation usually offers lower-level operations that the abstraction combines; here we've simplified it to a single deliver. By the way, a final send() with title() and body() as hooks is a Template Method inside the abstraction. And an Abstract Factory can be the one that picks which implementation to use.
And when you don't need it: if there's only one channel, or only one kind of reminder, a direct class is simpler. And if each channel needs different wording for each reminder, the two sides aren't independent: the bridge leaks, and per-channel templates will serve you better.
Code
SmsPaymentReminder.java
public String text(String name) {
// copied from SmsClassReminder… without the 160-character cut
return "Payment due: Hi " + name + ", your " + amount + " € fee is due on " + due + ". " + note;
}SmsPaymentReminder.java
public String text(String name) {
// copied from SmsClassReminder… without the 160-character cut
return "Payment due: Hi " + name + ", your " + amount + " € fee is due on " + due + ". " + note;
}Reminder.java
/** The abstraction: what a reminder says. How it travels is the channel's business. */
public abstract class Reminder {
private final Channel channel; // the bridge to the implementation
protected Reminder(Channel channel) {
if (channel == null) throw new InvalidReminderException("a reminder needs a channel");
this.channel = channel;
}
public final Delivery send(Member member) {
return channel.deliver(member, title(), body(member));
}
protected abstract String title();
protected abstract String body(Member member);
}Channel.java
/** The implementor: how a message reaches a member. It knows nothing about classes or payments. */
public interface Channel {
String name();
Delivery deliver(Member to, String title, String body);
}ClassReminder.java
@Override protected String title() { return "Class tomorrow"; }
@Override protected String body(Member m) {
return className + " at " + startsAt + ". See you on the floor, " + m.name() + "!";
}SmsChannel.java
@Override
public Delivery deliver(Member to, String title, String body) {
String text = title + ": " + body;
text = Texts.cut(text, LIMIT); // one rule, for every reminder
return new Delivery(name(), to.phone(), text);
}BridgeTest.java
Reminder classBySms = new ClassReminder(sms, "Hyrox prep", LocalTime.of(18, 0));
Reminder payBySms = new PaymentReminder(sms, new BigDecimal("89.00"), LocalDate.of(2026, 12, 1));
assertThat(classBySms.send(ana).text()).isEqualTo("Class tomorrow: Hyrox prep at 18:00. See you on the floor, Ana!");
assertThat(payBySms.send(ana).to()).isEqualTo("+34600000001");ReminderApiTest.java
mvc.perform(post("/api/reminders").contentType(MediaType.APPLICATION_JSON)
.content("{\"member\":\"leo\",\"kind\":\"class\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.channel").value("sms"))
.andExpect(jsonPath("$.to").value("+34600000002"));SpringDoesItTooTest.java
RestClient overJdk = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory()) // java.net.http.HttpClient underneath
.baseUrl("http://localhost:" + port).build();
RestClient overUrlConnection = RestClient.builder()
.requestFactory(new SimpleClientHttpRequestFactory()) // HttpURLConnection underneath
.baseUrl("http://localhost:" + port).build();
String a = overJdk.get().uri("/api/reminders/channels").retrieve().body(String.class);
String b = overUrlConnection.get().uri("/api/reminders/channels").retrieve().body(String.class);Reminder.java
/** The abstraction: what a reminder says. How it travels is the channel's business. */
public abstract class Reminder {
private final Channel channel; // the bridge to the implementation
protected Reminder(Channel channel) {
if (channel == null) throw new InvalidReminderException("a reminder needs a channel");
this.channel = channel;
}
public final Delivery send(Member member) {
return channel.deliver(member, title(), body(member));
}
protected abstract String title();
protected abstract String body(Member member);
}