Software engineering / Facade pattern

Facade pattern

Placing an order takes four classes and four calls in the right order. We put one simple front on that tangle, count what it saves, and compare it with an adapter.

Beginner7 steps~6 min
Client — code that wants an order placed
Facade — the one simple front
Subsystem class — one of the parts doing the real work
Adapter — converts one interface (step 7)
// THE SHORT VERSION

In 30 seconds

A facade is one simple object placed in front of a group of complicated ones. Instead of calling many classes in the right order, client code calls a single method, and the facade does the rest.

The classes behind it do not change, and they can still be used directly when needed. What changes is that clients depend on one thing instead of many, so changes behind the facade no longer spread.

It differs from an adapter, which converts one existing interface into another that the client already expects.

Facade pattern, step by step

STEPS01 / 07

Simplified: a real placeOrder must also handle failures, such as releasing the reserved stock when the card is declined. Hiding that handling is one more job a facade often takes on.

STEP 01 / 07 A job with many parts
THE MATH
reserve → charge → book → send receipt
  1. Step 1: A job with many parts. In an online shop, placing one order means talking to four separate classes: reserve the item in Stock, charge the card through Payments, book a parcel with Shipping, and have the Mailer send a receipt. Together they form a subsystem. The web shop has to know all four, their methods, and the right order to call them in.
  2. Step 2: Every client repeats it. The mobile app and the admin tool place orders too, so each of them repeats the same four calls. Three clients each tied to four classes makes twelve connections, and three copies of the recipe that must be kept in step. A change to any one part now ripples into every client.
  3. Step 3: Put a facade in front. A facade is a new class placed in front of the subsystem. It offers the one operation clients actually want, placeOrder, and inside it makes the four calls in the right order. Clients now talk only to the facade. The four classes behind it are not changed at all; they simply get a single, simpler front.
  4. Step 4: One call, same work. Look inside the facade. placeOrder contains exactly the four calls each client used to make, written once. The web shop calls one method and the facade passes the work on to Stock, Payments, Shipping and Mailer in turn. A facade adds no new abilities; it only gathers existing steps behind one name.
  5. Step 5: Less coupling. Coupling means how much one piece of code depends on the details of another. With the facade there are 3 + 4 = 7 connections instead of 12, and no client knows a subsystem class. When the shop moves to a new payment provider with a different method, only the facade is edited. Before, all three clients would have changed.
  6. Step 6: A front door, not a wall. A facade does not lock the subsystem away. The admin tool sometimes needs to resend a receipt, which the facade does not offer, so it calls the Mailer directly. That is fine: the facade is a convenient default for the common case. It should also stay thin, passing work on rather than growing into a class that does everything.
  7. Step 7: Facade versus adapter. Both patterns wrap other code, but for different reasons. A facade invents a new, simpler interface in front of many classes. An adapter makes one existing class fit an interface the client already expects: the client calls pay(30) in euros, and the adapter turns that into the old library’s makePayment(3000) in cents.
VALUES SO FAR
parts4 classessubsystem
recipe4 ordered callsclient
links····tangle
facade····front
links····result
swap····result
direct····note
facade····intent
adapter····intent

Spotted a mistake on this page?