Facade
- Intent
- Offer one simple door to a set of parts that together do something complicated, so whoever uses it doesn't have to know them or coordinate them.
- Application
- Signing up a member touches six parts of the system, in a set order, undoing what was done if the payment fails. Put it all behind one operation any client can use.
The front desk sends Ana · unlimited to FrontDeskController, highlighted in red, which calls all six subsystems itself: register(), subscribe(), welcome() and the rest.
The charge with tok_declined fails; MemberRegistry, AccessCards and Billing are highlighted: the member and their card stay even though nothing was paid.
OnboardingFacade appears, highlighted, wired to the six subsystems.
Billing, highlighted with the facade, answers payment declined; the facade calls revoke() on AccessCards and remove() on MemberRegistry.
The Spring context appears. MemberController and KioskController, highlighted, send join() to OnboardingFacade, wired to the six subsystems.
POST /api/members travels from the front desk to MemberController and join() to the facade, highlighted, which calls register(), issue(), subscribe() and welcome().
Inside the Spring context, ApplicationContext, highlighted, sends getBean() to BeanFactory, getMessage() to MessageSource, publishEvent() to ApplicationEventMulticaster and getResources() to ResourcePatternResolver.
Final view: the front desk and the visitor, both controllers, OnboardingFacade and the six subsystems, all inside the Spring context.
Step 1 of 8
Becoming a member
Ana wants to join the box. At the front desk, signing her up means:
- registering her as a member;
- giving her an access card;
- charging the first month;
- sending her the waiver to sign;
- booking her intro class;
- sending the welcome email.
Those are six different parts of the system. The first version, FrontDeskController, calls them one by one.
Step 2 of 8
Leo, halfway
Leo tries to join, but his bank card is declined. By then, the controller has already registered him and already handed him an access card, and it undoes nothing: Leo can walk into the box without having paid. The FrontDeskControllerTest test proves it.
And then the kiosk at the entrance arrives, which also signs up members. Do you copy the six steps? And the next time the order changes?
Step 3 of 8
One door
OnboardingFacade offers a single operation: join(JoinRequest). Inside, it coordinates the six parts:
- it registers, hands over the card and charges;
- then it sends the waiver, books the class and sends the welcome;
- it returns a
Welcomewith everything the member needs to know.
The subsystems don't change and don't know there's a facade. There's just one new place that knows how to use them together.
Step 4 of 8
Order matters, and so does undoing
The facade orders the steps on purpose:
- it checks the plan before anything else;
- what can be undone goes first (registering, handing over the card) and charging goes last in that block;
- if the charge fails, it takes the card back, deletes the registration and rethrows the error;
- what can't be undone, like emails, is only sent once the charge has gone through.
Once the charge goes through, the member is in: if the waiver, the booking or the email fails afterwards, the facade logs it to be retried by hand, but doesn't undo the sign-up. The onceItIsPaidAFailingFollowUpDoesNotUndoTheMembership test checks it.
Step 5 of 8
Two clients, one call
In Spring, the facade is just another @Service. MemberController (the front desk app, with JSON) and KioskController (the tablet's form) get it injected and make one call: join(...).
Neither of them knows the cards, the billing or the waiver exist. If the order changes tomorrow, the facade changes and the clients never notice.
Step 6 of 8
Ana, in
POST /api/members with Ana, the unlimited plan and a valid payment:
MemberControllercallsjoin(...).- The facade registers Ana, gives her card
CARD-0001and charges 89.00 €. - It sends the waiver, books her Saturday 10:00 class and sends the welcome.
- It answers 201 with all of that.
If the payment is declined, the answer is 402 and there's no trace of Ana left in the system.
Step 7 of 8
Spring already does it for you
The ApplicationContext acts as a facade. Behind a single interface there are several parts, and AbstractApplicationContext delegates each call to its own:
getBean(...), to itsBeanFactory;getMessage(...), to theMessageSource(Spring Boot sets it up on its own if there's amessages.properties);publishEvent(...), to theApplicationEventMulticaster;getResources(...), to aResourcePatternResolver.
The example test uses all four on the same object. And its "big" operation, refresh(), is like our join(): it sets up the bean factory, the post-processors, the MessageSource, events and the singletons in a fixed order.
Step 8 of 8
Recap
You started with a controller coordinating six parts and ended up with:
- A facade with one business operation,
join(). - The order and the compensation in one place.
- Several clients making a single call.
- Subsystems that are still there for anyone who needs them directly.
Don't mix it up with its neighbours: Adapter turns one interface into another that already exists; Facade creates a new, simpler one over several parts; with Mediator, the parts know the mediator and talk through it, while the subsystems behind a facade don't even know it exists. And a facade usually has a single instance: in Spring, a singleton bean.
And when you don't need it: if there's only one client and two calls, the facade is one class too many. And if it starts piling up unrelated operations, split it by use case before it knows everything.
Code
FrontDeskController.java
public Welcome join(JoinRequest req) {
Member member = registry.register(req.name(), req.email());
String card = cards.issue(member.id());
Subscription sub = billing.subscribe(member.id(), req.plan(), req.paymentToken());
waivers.send(member);
String introClass = intro.bookIntro(member.id());
mailer.welcome(member, card, introClass);
return new Welcome(member.id(), member.name(), card, sub.plan(), sub.monthlyPrice(), introClass);
}FrontDeskController.java
public Welcome join(JoinRequest req) {
Member member = registry.register(req.name(), req.email());
String card = cards.issue(member.id());
Subscription sub = billing.subscribe(member.id(), req.plan(), req.paymentToken());
waivers.send(member);
String introClass = intro.bookIntro(member.id());
mailer.welcome(member, card, introClass);
return new Welcome(member.id(), member.name(), card, sub.plan(), sub.monthlyPrice(), introClass);
}OnboardingFacade.java
public Welcome join(JoinRequest req) {
billing.priceOf(req.plan()); // fail fast on an unknown plan
Member member = registry.register(req.name(), req.email());
String card;
try {
card = cards.issue(member.id()); // reversible steps first…
} catch (RuntimeException e) {
undo(e, () -> registry.remove(member.id()));
throw e;
}
Subscription sub;
try {
sub = billing.subscribe(member.id(), req.plan(), req.paymentToken()); // …charge last
} catch (RuntimeException e) {
undo(e, () -> cards.revoke(card)); // undo what was done
undo(e, () -> registry.remove(member.id()));
throw e;
}
// Paid: from here on the member is in. Follow-ups that fail are logged, not undone.
followUp("waiver", () -> waivers.send(member));
String introClass = followUp("intro class", () -> intro.bookIntro(member.id()), "to be confirmed");
followUp("welcome email", () -> mailer.welcome(member, card, introClass));
return new Welcome(member.id(), member.name(), card, sub.plan(), sub.monthlyPrice(), introClass);
}OnboardingFacade.java
public Welcome join(JoinRequest req) {
billing.priceOf(req.plan()); // fail fast on an unknown plan
Member member = registry.register(req.name(), req.email());
String card;
try {
card = cards.issue(member.id()); // reversible steps first…
} catch (RuntimeException e) {
undo(e, () -> registry.remove(member.id()));
throw e;
}
Subscription sub;
try {
sub = billing.subscribe(member.id(), req.plan(), req.paymentToken()); // …charge last
} catch (RuntimeException e) {
undo(e, () -> cards.revoke(card)); // undo what was done
undo(e, () -> registry.remove(member.id()));
throw e;
}
// Paid: from here on the member is in. Follow-ups that fail are logged, not undone.
followUp("waiver", () -> waivers.send(member));
String introClass = followUp("intro class", () -> intro.bookIntro(member.id()), "to be confirmed");
followUp("welcome email", () -> mailer.welcome(member, card, introClass));
return new Welcome(member.id(), member.name(), card, sub.plan(), sub.monthlyPrice(), introClass);
}MemberController.java
@PostMapping("/api/members")
@ResponseStatus(HttpStatus.CREATED)
public Welcome join(@RequestBody JoinRequest request) {
return onboarding.join(request);
}MemberApiTest.java
mvc.perform(post("/api/members").contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"Ana\",\"email\":\"ana.api@box.example\",\"plan\":\"unlimited\",\"paymentToken\":\"tok_visa\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.monthlyPrice").value(89.00))
.andExpect(jsonPath("$.introClass").value("Saturday 10:00"));SpringDoesItTooTest.java
OnboardingFacade facade = context.getBean(OnboardingFacade.class); // beans
String hello = context.getMessage("welcome", new Object[] {"Ana"}, Locale.ENGLISH); // messages
Resource[] files = context.getResources("classpath*:messages*.properties"); // resources
context.publishEvent(new MemberJoined("Ana")); // eventsOnboardingFacade.java
public Welcome join(JoinRequest req) {
billing.priceOf(req.plan()); // fail fast on an unknown plan
Member member = registry.register(req.name(), req.email());
String card;
try {
card = cards.issue(member.id()); // reversible steps first…
} catch (RuntimeException e) {
undo(e, () -> registry.remove(member.id()));
throw e;
}
Subscription sub;
try {
sub = billing.subscribe(member.id(), req.plan(), req.paymentToken()); // …charge last
} catch (RuntimeException e) {
undo(e, () -> cards.revoke(card)); // undo what was done
undo(e, () -> registry.remove(member.id()));
throw e;
}
// Paid: from here on the member is in. Follow-ups that fail are logged, not undone.
followUp("waiver", () -> waivers.send(member));
String introClass = followUp("intro class", () -> intro.bookIntro(member.id()), "to be confirmed");
followUp("welcome email", () -> mailer.welcome(member, card, introClass));
return new Welcome(member.id(), member.name(), card, sub.plan(), sub.monthlyPrice(), introClass);
}