Facade
A facade is a single, simpler object that fronts a cluster of collaborators. Callers use the facade; they do not wire the subsystems themselves.
Detailed Explanation
Checkout needs inventory, payments, email, and an audit log. If every view constructs those four clients and calls them in the right order, every view knows too much. A CheckoutService with place_order(cart, user) is a facade. Inside, it talks to the four systems. Outside, there is one method and one set of errors.
A facade is not an adapter. An adapter changes one interface into another. A facade hides many interfaces behind one. It is not a god object if it only orchestrates and leaves policy in the subsystems. When the facade grows if for every product line, it is time to split.
In Python a facade is often a module: checkout.place_order(...) with private helpers. A class is better when the facade has injectable dependencies. Tests construct CheckoutService(inventory=fake, payments=fake).
Facades are how you keep frameworks out of the domain. The facade talks to Django's ORM or to Stripe; the domain functions talk to the facade's types. That is also hexagonal architecture in one word.
Do not make a facade that only forwards one method to one object. That is an extra name. Wait until the third collaborator, or until the call sequence is easy to get wrong.
Document the transaction boundary. If place_order charges and then fails to decrement stock, the facade is where you retry, compensate, or raise. Callers should not be expected to undo.
Code Examples
class Checkout:
def __init__(self, inventory, payments, mail):
self.inventory = inventory
self.payments = payments
self.mail = mail
def place_order(self, sku: str, cents: int, email: str) -> str:
self.inventory.reserve(sku)
payment_id = self.payments.charge(cents)
self.mail.send(email, f"paid {payment_id}")
return payment_id
class Inv:
def reserve(self, sku: str) -> None:
pass
class Pay:
def charge(self, cents: int) -> str:
return f"pay-{cents}"
class Mail:
def send(self, to: str, body: str) -> None:
pass
print(Checkout(Inv(), Pay(), Mail()).place_order("A-1", 100, "a@b.c"))Common Gotchas
A facade that leaks subsystem types
callers still import Stripe. Return your own ids and errors.
Hidden partial success
charge succeeded, email failed, and the facade returns OK. Say what is guaranteed.
God facade
fifty methods on one class. Split by use case.
A package `__init__.py` that re-exports is a facade for imports
keep it thin.