Back to results for “Design Patterns in JavaScript”
Node.js › Architecture & Design Patterns

What is a Facade pattern, and how might it be used to simplify a Node.js application's interaction with several third-party SDKs?

Advanced

Answer

A Facade provides a single, simplified interface over a more complex subsystem of classes or libraries -- rather than every part of an application directly importing and configuring multiple third-party SDKs (payment, email, SMS, analytics) with their own inconsistent APIs, a Facade wraps them behind one consistent internal interface, isolating the rest of the codebase from those libraries' specific quirks and making it easier to later swap out or upgrade an underlying SDK.
class NotificationFacade {
  constructor() { this.email = new SendGridClient(); this.sms = new TwilioClient(); }
  async notifyUser(user, message) {
    if (user.prefersEmail) return this.email.send(user.email, message);
    return this.sms.send(user.phone, message);
  }
}

// Rest of the app only ever calls this simple interface
await notificationFacade.notifyUser(user, 'Your order has shipped');
Real-world example An application originally calling the Twilio SDK directly from a dozen different files is refactored to go through a single NotificationFacade, so that a later migration from Twilio to a different SMS provider only requires changing that one facade class instead of a dozen call sites.

Common follow-ups: What's the difference between a Facade and an Adapter pattern, given both wrap another interface?;At what point does a facade risk becoming an unwieldy 'god object' itself?