Facade Pattern in Python
Facade coordinates several lower-level services so callers do not need to understand their workflow.
What is Facade Pattern?
Facade coordinates several lower-level services so callers do not need to understand their workflow.
Provide one simple interface to a complex subsystem.
When should you use it?
- Solve recurring object-design problems with proven structures.
- Decouple responsibilities so code is easier to extend and test.
- Use shared design vocabulary when discussing architecture with a team.
Example code
main.py
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())
Expected output
Order placed
How it works
OrderFacade hides inventory and payment coordination behind place_order.
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.