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.
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
| Aspect | Adapter | Bridge |
|---|---|---|
| Timing | After the fact β glue between existing code | Upfront β designed into the architecture |
| Purpose | Make incompatible interfaces work together | Separate abstraction from implementation so both vary independently |
| Interfaces | Adaptee's interface already exists and cannot change | You define both the abstraction and implementor interfaces |
| Dimensions | One: the adaptee β target conversion | Two: abstraction hierarchy Γ implementation hierarchy |
| Lifespan | Often 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
| 1 | // Existing code expects this interface |
| 2 | interface PaymentGateway { |
| 3 | charge(total: number, currencyCode: string): Promise<{ transactionId: string }>; |
| 4 | } |
| 5 | |
| 6 | // Razorpay has a different interface we cannot change |
| 7 | class 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 |
| 14 | class 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
| 1 | // Implementor interface (delivery channel) |
| 2 | interface NotificationChannel { |
| 3 | send(recipient: string, message: string): Promise<void>; |
| 4 | } |
| 5 | |
| 6 | class EmailChannel implements NotificationChannel { |
| 7 | async send(recipient: string, message: string) { |
| 8 | console.log(`Email to ${recipient}: ${message}`); |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | class SMSChannel implements NotificationChannel { |
| 13 | async send(recipient: string, message: string) { |
| 14 | console.log(`SMS to ${recipient}: ${message}`); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | // Abstraction (notification type) |
| 19 | abstract class Notification { |
| 20 | constructor(protected channel: NotificationChannel) {} |
| 21 | abstract notify(recipient: string, content: string): Promise<void>; |
| 22 | } |
| 23 | |
| 24 | class 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 | |
| 31 | class InfoNotification extends Notification { |
| 32 | async notify(recipient: string, content: string) { |
| 33 | await this.channel.send(recipient, content); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Mix and match |
| 38 | const urgentSms = new UrgentNotification(new SMSChannel()); |
| 39 | const infoEmail = new InfoNotification(new EmailChannel()); |
Decision Rules
| Signal | Choose | Why |
|---|---|---|
| You have two existing systems that don't match | Adapter | Retrofit compatibility |
| You are designing a new system with two independent dimensions | Bridge | Prevent a class explosion |
| The "other side" is a third-party SDK you can't change | Adapter | You adapt to their interface |
| You own both sides and want them to evolve separately | Bridge | Intentional decoupling |
| You need a temporary compatibility layer during migration | Adapter | It can be removed later |
| Adding a new variant shouldn't require touching existing code | Bridge | Open for extension on both axes |
The Trick Question
"Your logging library uses
log(level, message)but your application code callslogger.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.