Python Design Patterns

Facade Pattern in Python

Give a complex set of services one simple entry point, so callers place an order with a single call.

  • Advanced
  • 2 min read
  • 1 runnable example
What you'll learn
  • Hiding subsystem details
  • Coordinating several services
  • Keeping callers simple
shop.place_order()
  • OrderFacade
    • Inventory
    • Payment
One call in, three coordinated calls out: "Order placed".

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.

From a hotel reception to code
OrderFacade().place_order()🛎️ Ask the reception for one thing
Inventory().reserve()📦 One department behind the desk
Payment().charge()💳 Another department
"Order placed"🗝️ You get the result, not the details
One simple method in front of several subsystems.
In one sentence

The Facade pattern provides one simple interface to a set of more complex subsystems and coordinates them behind that interface.

Key points

  1. Inventory and Payment are separate subsystems with their own methods.
  2. place_order() calls reserve() and charge() in the required order.
  3. The caller only needs OrderFacade().place_order().
  4. 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

Run 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())
Get a Run button for this example

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.

  1. Replace the sample domain while preserving the pattern structure.
  2. Compare the pattern with a simpler implementation and explain the tradeoff.
  3. Write tests for each participant before adding another implementation.
Run in the Python compiler →

Common mistakes

Letting callers repeat the workflow

Each caller coordinating the subsystems may get the order wrong.

Problem
Payment().charge()
Inventory().reserve()  # charged before checking stock
Fix
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