Facade
Intent
Offer one simple entry step that sequences a handful of subsystem steps, so callers start a complex job without learning each device.
Motivation
Movie night needs three gadgets driven in order: dim the lights, lower the screen, then play picture with sound. Every viewer who drives the projector, screen, and speakers directly must remember the order and each device’s quirks, and guests simply give up. A home-theater facade exposes one start_movie step that runs the dim-lower-play sequence, so the living room behaves like one machine.
Structure
flowchart LR Viewer --> Theater[Home Theater Facade] Theater --> Lights Theater --> Screen Theater --> Sound
- Facade (Home Theater) — owns the
start_moviesequence and hides device order. - Subsystems (Lights, Screen, Sound/Projector) — each performs its own single step.
- Client (Viewer) — calls one step instead of coordinating three devices.
Consequences
- Benefit: the common path becomes one memorable call; subsystem ordering lives in exactly one place.
- Cost / trade-off: the facade can hide power features — unusual sequences still need direct subsystem access.
Basic Recap
Facade offers a focused entry point to a set of subsystem operations. It captures a common coordination sequence so clients do not need to know every collaborator or low-level detail. The subsystems remain available for specialized use, and the facade does not have to hide them completely. Its purpose is a simpler boundary for a recurring task, not universal ownership of all application behavior.
Complex Scenario
A customer account service provides an AccountClosure facade for a support
workflow. Closing an account involves identity sessions, active subscriptions,
ledger holds, data-export eligibility, and an audit record. Support code calls
one operation and receives a structured closure status instead of coordinating
five APIs. The facade establishes the required sequence and translates subsystem
results into a consistent decision about what still needs attention.
Edge cases
- Partial failure: Subscriptions may be cancelled before the ledger rejects closure; the result must record completed work and required compensation rather than pretending the whole operation rolled back.
- Retries: A repeated request should use the same closure key so session revocation and cancellation are resumed or safely recognized as already done.
- Concurrency: Two support agents closing one account need a version check or per-account lock to prevent contradictory decisions based on stale balances.
A distributed facade coordinates progress; it is not automatically a transaction. A durable workflow may be needed when closure must survive process restarts.
Sketch (illustrative pseudocode, not runnable):
AccountClosure.close(accountId, requestKey):
progress = closureJournal.beginOrResume(accountId, requestKey)
assertAccountVersionUnchanged(progress)
progress.runStep(revokeSessions)
progress.runStep(cancelSubscriptions)
ledgerDecision = progress.runStep(releaseEligibleHolds)
if ledgerDecision.blocked:
return ClosureBlocked(progress.completedSteps, ledgerDecision)
progress.runStep(writeAuditRecord)
return ClosureCompleted(progress.id)
When to Use / When Not to Use
Use when:
- One routine sequence covers most callers and subsystem order is easy to get wrong.
- You want a stable entry point while subsystems keep evolving underneath.
Avoid when:
- Callers mostly need custom orders or fine-grained control — the facade becomes a pass-through they bypass.
- The “simplification” keeps growing until it mirrors every subsystem knob.
Examples
Links to runnable code (same scenario, four languages):
examples/elixir/facade/examples/dart/facade/examples/go/facade/examples/rust/facade/
Expected outcome: one start_movie call prints the dim, lower, and play steps in order.
Anti-pattern / Failure Mode
-
Misuse: A facade that grows every subsystem method — a new god object.
-
Review signal: Callers bypassing the facade because it hides what they need.
-
Misuse symptom: Every subsystem call and every business rule migrates into one facade until it becomes a god service with hundreds of dependencies. Callers then bypass it unpredictably because its broad contract no longer matches any focused task.
-
Review signal: Watch for unrelated methods sharing one facade, constructor lists that continually grow, and tests requiring the entire subsystem graph for a simple branch. Closure dashboards that report only success or failure, with no durable step progress, reveal that the friendly API is concealing partial completion rather than managing it.
Related
- Adapter — simplify vs translate.
- Mediator — one call driving a subsystem vs peers talking through a hub.
- API Consistency — facades should stay uniform.
Exercise
Add a shut_down step that reverses the sequence (stop playback, raise screen, brighten lights). Callers must still use one call, and the start order must stay unchanged.
Self-check
- What does the facade own?
Answer
The ordering and coordination of subsystem steps — not the device behavior itself. - Facade vs Adapter: what differs?
Answer
Facade simplifies many steps into one; Adapter translates one interface into another. - When should callers bypass it?
Answer
For unusual sequences or fine control the single routine does not cover.