Facade: a simple door to a complex system
- Lesson 14 of 32
- 10 min
- Beginner
- 12 steps
Buying in a shop is four calls in a precise order, copied on every screen. You will gather them behind a facade and see what you gain and what you pay.
- Classes checkout knows
- 1
- request
- response
- failed request
Step 1 of 12
Problem
Buying is four calls in order
An online shop confirms an order like this: Inventory reserves the product, Billing charges, Shipping schedules the delivery and Mail sends the email with the invoice and the tracking number.
The checkout page makes all four calls itself, in that order. To do that it knows the four classes, their methods and which value each one hands to the next.
4999 is cents and SKU-1 is a product: example values.
Step 2 of 12
Problem
A client changes the order and something breaks
Now the mobile app sells too. Someone copies the steps but schedules the delivery before charging. Look at the two marked lines.
If the card is declined, charge throws when the delivery is already scheduled: a parcel is on its way that nobody paid for. The right order lived in the head of whoever wrote the first checkout.
What changed
- `schedule` before `charge`
Step 3 of 12
Solution
One class does the steps for you
CheckoutFacade offers a single method: placeOrder(sku, cents). Inside, it calls Inventory, Billing, Shipping and Mail in the right order and returns the invoice and the tracking number.
That is a facade: a simple door in front of a subsystem with many parts. It receives the four objects in its constructor; the panel only shows the method.
Checkout now knows one class instead of four.
What changed
- CheckoutFacade.placeOrder
- 4 calls in checkout
Step 4 of 12
Technique
What checkout writes
From outside, buying is one line: checkout.placeOrder("SKU-1", 4999). The answer carries invoice and tracking, the two values the screen needs to show.
Checkout no longer knows that Shipping exists or in which order the steps go. If those methods are renamed tomorrow, you edit the facade and the screen stays the same.
What changed
- checkout.placeOrder(...)
Step 5 of 12
Solution
A new step changes one place
The business wants a fraud check before charging. FraudCheck appears, with a check(cents) method.
Without a facade, you would open every screen that sells and put the call in the exact spot. With a facade, you add one line to placeOrder, between the reservation and the charge.
Checkout and the mobile app do not change: they still call placeOrder and still know a single class.
What changed
- FraudCheck.check
- CheckoutFacade.placeOrder
Step 6 of 12
Concept
The subsystem is still in reach
A facade does not lock away the subsystem. The support team needs a manual charge, with no shipping and no email: it calls Billing directly.
The facade covers the common case; whoever needs fine control uses the classes below. If the facade were the only way in, you would have to add a method to it for every odd case.
Step 7 of 12
Technique
A facade that already exists: SLF4J
SLF4J calls itself the «Simple Logging Facade for Java». Your code logs against one API, and the library that really writes the logs (Logback, Log4j or java.util.logging) is picked at deployment.
So your code is not tied to one logging backend. In the scene, your app only talks to SLF4J and, in this example, the logs end up in Logback.
Step 8 of 12
Trade-off
When a step fails halfway
placeOrder reserved the product and then the charge fails. The stock stays set aside for an order that does not exist.
The facade does not fix that on its own: something has to undo what already happened. Here the facade itself does it, releasing the reservation before rethrowing the error (release is an example method).
When the steps live in different services, that problem has its own name: you will meet it in the sagas lesson of the System Design course.
What changed
- try / catch with `release`
Step 9 of 12
Trade-off
When the facade starts doing everything
It is handy to add every new thing there: refunds, cancellations, coupons, reports, CSV export. Soon CheckoutFacade knows the whole system and every change goes through the same file.
That is no longer a facade but a god object: many reasons to change in one place.
The way out is to split it by use case, for example CheckoutFacade and RefundFacade, each one small and with few dependencies.
Step 10 of 12
Concept
Three wrong ideas
It does not hide the subsystem completely: support still calls Billing. It is not where business rules live: it only orders calls to classes that already know how to do their part.
And you do not need one facade for the whole system: one per use case stays small and easy to read.
Step 11 of 12
Trade-off
When a facade is too much
If buying were just billing.charge(cents), a payments.charge(cents) facade that forwards it is one more layer to read and to step through when debugging, and it hides nothing.
It does not help either if callers need fine control almost every time: you would end up copying every subsystem method into the facade.
A facade pays off when there are several steps, in an order that matters, repeated across several clients.
Step 12 of 12
Summary
A simple door, the system is still there
When several clients repeat a sequence of calls to a subsystem, put that sequence in a facade with one method for the common case. Callers know one class and the order lives in one place.
The subsystem stays available for whoever needs more control. Watch that the facade does not grow until it knows everything, and skip it if the system is already simple.
← → step · R replay · Space pause
You will learn
- Spot the problem: a sequence of calls to a subsystem repeated in every client. (covered)
- Put that sequence behind one simple method: the facade. (covered)
- Keep the subsystem available for whoever needs fine control. (covered)
- Recognise a real facade: SLF4J. (covered)
- See when the facade turns into a god object. (covered)
- Know when a facade is too much. (covered)