7
Back to Boss Battles

BridgeVSAdapter

The Problem

Scenario A β€” Adapter territory: Your payment service has been using Stripe for years. Now the business wants to add Razorpay. Stripe returns { amount_received, currency } but your codebase expects { total, currencyCode }. You cannot change either side β€” you need a wrapper to translate Razorpay's interface into what your code already expects.

Scenario B β€” Bridge territory: You are building a notification system from scratch. Notifications can be Urgent or Informational (the abstraction), and they can be delivered via Email, SMS, or Push (the implementation). You want to mix and match freely: Urgent+SMS, Informational+Email, etc. You design the separation upfront so neither dimension locks in the other.


Why They Look Alike

Both use composition to delegate work to another object behind an interface. Both have a "wrapper" that holds a reference to the "real" implementation.

Rendering diagram…

Both introduce an indirection layer. The class diagrams look nearly identical: one object holds a reference to another and delegates. The difference is when and why you introduce the indirection.


The Core Difference

AspectAdapterBridge
TimingAfter the fact β€” glue between existing codeUpfront β€” designed into the architecture
PurposeMake incompatible interfaces work togetherSeparate abstraction from implementation so both vary independently
InterfacesAdaptee's interface already exists and cannot changeYou define both the abstraction and implementor interfaces
DimensionsOne: the adaptee β†’ target conversionTwo: abstraction hierarchy Γ— implementation hierarchy
LifespanOften temporary (until the legacy system is replaced)Permanent architectural decision

One sentence: Adapter is a retrofit patch; Bridge is an architectural split.


Side-by-Side Code

Adapter β€” Wrapping a legacy payment API

typescript
1// Existing code expects this interface
2interface PaymentGateway {
3 charge(total: number, currencyCode: string): Promise<{ transactionId: string }>;
4}
5
6// Razorpay has a different interface we cannot change
7class RazorpaySDK {
8 createOrder(amount_paise: number, currency: string): Promise<{ id: string }> {
9 return Promise.resolve({ id: `rzp_${Date.now()}` });
10 }
11}
12
13// Adapter: translate Razorpay into our expected interface
14class RazorpayAdapter implements PaymentGateway {
15 constructor(private sdk: RazorpaySDK) {}
16
17 async charge(total: number, currencyCode: string) {
18 const result = await this.sdk.createOrder(total * 100, currencyCode);
19 return { transactionId: result.id };
20 }
21}

Bridge β€” Notification system designed for two dimensions

typescript
1// Implementor interface (delivery channel)
2interface NotificationChannel {
3 send(recipient: string, message: string): Promise<void>;
4}
5
6class EmailChannel implements NotificationChannel {
7 async send(recipient: string, message: string) {
8 console.log(`Email to ${recipient}: ${message}`);
9 }
10}
11
12class SMSChannel implements NotificationChannel {
13 async send(recipient: string, message: string) {
14 console.log(`SMS to ${recipient}: ${message}`);
15 }
16}
17
18// Abstraction (notification type)
19abstract class Notification {
20 constructor(protected channel: NotificationChannel) {}
21 abstract notify(recipient: string, content: string): Promise<void>;
22}
23
24class UrgentNotification extends Notification {
25 async notify(recipient: string, content: string) {
26 await this.channel.send(recipient, `🚨 URGENT: ${content}`);
27 await this.channel.send(recipient, `Reminder: ${content}`);
28 }
29}
30
31class InfoNotification extends Notification {
32 async notify(recipient: string, content: string) {
33 await this.channel.send(recipient, content);
34 }
35}
36
37// Mix and match
38const urgentSms = new UrgentNotification(new SMSChannel());
39const infoEmail = new InfoNotification(new EmailChannel());

Decision Rules

SignalChooseWhy
You have two existing systems that don't matchAdapterRetrofit compatibility
You are designing a new system with two independent dimensionsBridgePrevent a class explosion
The "other side" is a third-party SDK you can't changeAdapterYou adapt to their interface
You own both sides and want them to evolve separatelyBridgeIntentional decoupling
You need a temporary compatibility layer during migrationAdapterIt can be removed later
Adding a new variant shouldn't require touching existing codeBridgeOpen for extension on both axes

The Trick Question

"Your logging library uses log(level, message) but your application code calls logger.info(msg), logger.error(msg). You write a wrapper class. Is this Adapter or Bridge?"

This is Adapter. The logging library already exists with its own interface. You're wrapping it to match the interface your code expects. You didn't design the separation β€” you're fixing a mismatch. If instead you were designing a logging system from scratch where the formatting (JSON, plain text) and the output (file, console, cloud) are independent dimensions, that would be Bridge.


Interview Angles

Q: Can an Adapter evolve into a Bridge? Yes. You start with an Adapter to wrap a legacy service. Over time you realize you need multiple implementations (Stripe, Razorpay, PayPal) and multiple abstraction levels (one-time charge, subscription, refund). At that point you've grown into a Bridge.

Q: Is NestJS's @Injectable() service with an injected interface an Adapter or Bridge? It depends on intent. If the interface exists because a third-party SDK doesn't match your code β€” Adapter. If you designed the interface to separate "what" (service) from "how" (implementation), and both sides can vary β€” Bridge.

Q: How do you explain the difference in one sentence in an interview? "Adapter is a bandage for existing code that doesn't fit; Bridge is a planned separation so two dimensions can change independently."

Q: Which is more common in backend code? Adapter, by far. Most backend code wraps third-party APIs (payment, email, cloud). Bridge appears in frameworks and libraries where the designer needs two axes of variation.