AdapterVSFacade
The Problem
Scenario A — Adapter territory:
You are integrating a third-party SMS library (Twilio) into your notification system. Your system expects a MessageSender interface with send(to, body), but Twilio's API has client.messages.create({ to, from, body }). You need a wrapper that translates your interface to Twilio's.
Scenario B — Facade territory:
Placing an order involves 5 subsystems: inventory check, payment processing, fraud detection, shipping calculation, and email confirmation. Each has its own API. You want a single OrderService.placeOrder() method that orchestrates all 5 so the controller doesn't need to know the details.
Why They Look Alike
Both wrap other code behind a simpler interface. Both reduce coupling between the client and the underlying implementation. People describe both as "a wrapper" and get confused.
The Fundamental Difference
Adapter makes an incompatible interface compatible. Facade simplifies a complex subsystem into one entry point.
Adapter: "I need THIS interface, but I have THAT one — translate." Facade: "I have 5 things to coordinate — give me one button."
Decision Rule
| Clue | Pattern |
|---|---|
| "This library's API doesn't match our interface" | Adapter |
| "Too many subsystems to call individually" | Facade |
| "Need to swap one implementation for another" | Adapter |
| "Simplify a complex multi-step workflow" | Facade |
| "Wraps ONE class to match an expected interface" | Adapter |
| "Wraps MULTIPLE classes behind a unified API" | Facade |
| "Implements an existing interface" | Adapter |
| "Defines a NEW simplified interface" | Facade |
Side-by-Side Code
Adapter — translate one interface to another
| 1 | // Your system's interface |
| 2 | interface MessageSender { |
| 3 | send(to: string, body: string): Promise<string>; |
| 4 | } |
| 5 | |
| 6 | // Third-party Twilio client (incompatible interface) |
| 7 | class TwilioClient { |
| 8 | constructor(private accountSid: string, private authToken: string) {} |
| 9 | |
| 10 | messages = { |
| 11 | create: async (opts: { to: string; from: string; body: string }) => { |
| 12 | console.log(`Twilio sending to ${opts.to}: ${opts.body}`); |
| 13 | return { sid: "SM123", status: "sent" }; |
| 14 | }, |
| 15 | }; |
| 16 | } |
| 17 | |
| 18 | // Adapter: translates MessageSender → TwilioClient |
| 19 | class TwilioAdapter implements MessageSender { |
| 20 | private client: TwilioClient; |
| 21 | private fromNumber: string; |
| 22 | |
| 23 | constructor(sid: string, token: string, from: string) { |
| 24 | this.client = new TwilioClient(sid, token); |
| 25 | this.fromNumber = from; |
| 26 | } |
| 27 | |
| 28 | async send(to: string, body: string): Promise<string> { |
| 29 | const result = await this.client.messages.create({ |
| 30 | to, |
| 31 | from: this.fromNumber, |
| 32 | body, |
| 33 | }); |
| 34 | return result.sid; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Usage — your system only knows MessageSender |
| 39 | function notify(sender: MessageSender, to: string, msg: string) { |
| 40 | return sender.send(to, msg); |
| 41 | } |
| 42 | |
| 43 | const sms = new TwilioAdapter("sid", "token", "+1234567890"); |
| 44 | notify(sms, "+0987654321", "Your order shipped!"); |
Facade — simplify a complex subsystem
| 1 | // Multiple subsystems |
| 2 | class InventoryService { |
| 3 | check(productId: string): boolean { |
| 4 | console.log(`Checking inventory for ${productId}`); |
| 5 | return true; |
| 6 | } |
| 7 | } |
| 8 | |
| 9 | class PaymentService { |
| 10 | charge(userId: string, amount: number): string { |
| 11 | console.log(`Charging ${userId} $${amount}`); |
| 12 | return "txn_123"; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | class FraudDetection { |
| 17 | screen(userId: string, amount: number): boolean { |
| 18 | console.log(`Fraud check for ${userId}`); |
| 19 | return true; // passes |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | class ShippingService { |
| 24 | calculate(productId: string): number { |
| 25 | return 5.99; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | class EmailService { |
| 30 | sendConfirmation(userId: string, orderId: string) { |
| 31 | console.log(`Email sent to ${userId} for order ${orderId}`); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // Facade: one method orchestrates all subsystems |
| 36 | class OrderFacade { |
| 37 | constructor( |
| 38 | private inventory: InventoryService, |
| 39 | private payment: PaymentService, |
| 40 | private fraud: FraudDetection, |
| 41 | private shipping: ShippingService, |
| 42 | private email: EmailService, |
| 43 | ) {} |
| 44 | |
| 45 | async placeOrder(userId: string, productId: string, amount: number) { |
| 46 | if (!this.inventory.check(productId)) throw new Error("Out of stock"); |
| 47 | if (!this.fraud.screen(userId, amount)) throw new Error("Fraud detected"); |
| 48 | |
| 49 | const shipping = this.shipping.calculate(productId); |
| 50 | const txnId = this.payment.charge(userId, amount + shipping); |
| 51 | this.email.sendConfirmation(userId, txnId); |
| 52 | |
| 53 | return { txnId, total: amount + shipping }; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Usage — controller only talks to the facade |
| 58 | const facade = new OrderFacade( |
| 59 | new InventoryService(), |
| 60 | new PaymentService(), |
| 61 | new FraudDetection(), |
| 62 | new ShippingService(), |
| 63 | new EmailService(), |
| 64 | ); |
| 65 | facade.placeOrder("user_1", "prod_42", 29.99); |
Key difference: Adapter wraps one thing to match an existing interface. Facade wraps many things behind a new, simpler interface.
Scenario Quiz
?Knowledge Check
1You switch from AWS SES to SendGrid for email. Your codebase uses an EmailProvider interface. You write a SendGridEmailProvider that translates calls. Which pattern?
2Your user registration involves: create user record, hash password, send welcome email, create default settings, log analytics event. You wrap it in UserRegistrationService.register(). Which pattern?
3A legacy payment module uses XML-based methods. Your new system expects JSON-based PaymentGateway interface. You write a wrapper. Which pattern?
4A NestJS controller calls 4 different services to handle a complex endpoint. You extract the orchestration into a single service method. Which pattern?
5You wrap a Mongoose model to implement your Repository interface (find, save, delete methods). Which pattern?
6Can a class be both an Adapter and a Facade?
7A video encoding pipeline: upload → transcode → thumbnail → store → notify. You create VideoProcessor.process() that runs all steps. Which pattern?
8TypeORM expects a DataSource interface. You write a wrapper for a custom in-memory database that has a different API. Which pattern?