Facade Pattern in Python
Give a complex set of services one simple entry point, so callers place an order with a single call.
- Hiding subsystem details
- Coordinating several services
- Keeping callers simple
OrderFacadeInventoryPayment
One simple door to a complex system
The problem
Placing an order may involve checking stock, charging a card and arranging shipping, each with its own class and rules. If every caller must coordinate all of them in the right order, mistakes are easy. A facade offers one simple entry point.
Think of it like this
A facade is like a hotel reception desk. You ask for one thing, a room, and the receptionist deals with housekeeping, billing and keys behind the scenes. You never talk to each department yourself.
OrderFacade().place_order()🛎️ Ask the reception for one thingInventory().reserve()📦 One department behind the deskPayment().charge()💳 Another department"Order placed"🗝️ You get the result, not the detailsThe Facade pattern provides one simple interface to a set of more complex subsystems and coordinates them behind that interface.
Key points
InventoryandPaymentare separate subsystems with their own methods.place_order()callsreserve()andcharge()in the required order.- The caller only needs
OrderFacade().place_order(). - The subsystems still exist for code that needs them directly.
When should you use it?
- Hiding a complicated workflow behind one call, such as checkout or sign-up.
- Giving a large library a small, task-focused interface.
- Reducing how many classes the rest of the program depends on.
When not to use it
- Callers genuinely need fine control over each subsystem.
- The facade starts collecting unrelated tasks and becomes a giant class.
Example code
class Inventory:
# Facade: expose one simple operation over several services.
def reserve(self): return True
class Payment:
def charge(self): return True
class OrderFacade:
def place_order(self):
# Coordinate subsystem operations in the required order.
if Inventory().reserve() and Payment().charge():
return "Order placed"
return "Order failed"
print(OrderFacade().place_order())
Result of running the code
Order placed
Code explanation
Inventory and Payment each handle one part of an order. OrderFacade.place_order calls reserve() first and charge() only if reservation succeeds, which keeps the steps in the right order. Callers use this single method and never need to know which subsystems are involved.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Letting callers repeat the workflow
Each caller coordinating the subsystems may get the order wrong.
Payment().charge()
Inventory().reserve() # charged before checking stock
OrderFacade().place_order()
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges