Video in progress

Strong Junior 🧭 Design patterns

How do you tell Adapter and Facade apart? They both "wrap", after all.

An Adapter fits one existing interface to a different, expected interface, and its goal is compatibility. A Facade builds a new, simpler interface on top of a complex system made of several classes, and its goal is simplicity.

📖 Answer

You can tell them apart with a simple question: "does the interface the client expects already exist?"

  • Yes, it exists and the external class doesn't match it: that's an Adapter. For example, you have a PaymentGateway interface and you wrap a new bank's SDK to fit it.
  • No, you are inventing a new convenient API yourself, because inside you'd have to call 5 services in the right order: that's a Facade.

Another difference is how many things get wrapped. An Adapter usually wraps one object. A Facade brings many together.

// Adapter: one SDK -> our interface
class PaymeAdapter implements PaymentGateway { ... }

// Facade: several services -> one scenario
class CheckoutFacade {
    OrderId placeOrder(Cart cart) {
        stock.reserve(cart);
        var pay = gateway.charge(cart.total());
        return orders.create(cart, pay);
    }
}

In practice both live side by side in one project: a Facade uses Adapters inside.