Facade

Gives one simple interface to a complicated set of classes.

Why it matters

Using a complex subsystem directly means every caller must know many classes and the right order to call them, and that knowledge gets copied everywhere.

The idea

Placing an order may involve inventory, payment, shipping and notification classes. A CheckoutFacade with one place_order() method hides those steps, so callers use one method.

A hotel concierge: you ask for 'dinner and a taxi at 8', and they deal with the restaurant and the taxi company.

A concierge knows everything. A facade that keeps growing becomes a 'god class' that does too much, so keep it thin and let it delegate to the subsystems.

How it works

  1. Identify the task callers keep repeating: placing an order.
  2. Create a facade class that holds the subsystem objects.
  3. Give it one method per common task, which calls the subsystem pieces in the right order.
  4. Callers use the facade; advanced callers can still use the subsystem directly.

Example

A video-conversion library: convert(file, 'mp4') hides the codecs, buffers and audio mixing.

Key points

Common mistakes

Not true: A facade adds new features to the subsystem it wraps.
In fact: A facade adds no features. It offers a simpler way in to what the subsystem already does.

In the interview

Facade gives callers one simple method, like place_order(), over a complex subsystem of inventory, payment and shipping classes, which reduces coupling. I keep it thin so it doesn't become a god class.

Part of Unit 6: Design patterns in the free System Design course.