StructuralPythonverifiedVerified

Facade Pattern in Python

Provides a simplified, unified interface to a complex subsystem, hiding its internal complexity from clients.

How to Implement the Facade Pattern in Python

1Step 1: Define the complex subsystem classes

class SubsystemA:
    def operation_a(self) -> str:
        return "SubsystemA: ready"

    def operation_a2(self) -> str:
        return "SubsystemA: go!"


class SubsystemB:
    def operation_b(self) -> str:
        return "SubsystemB: ready"

    def operation_b2(self) -> str:
        return "SubsystemB: fire!"

2Step 2: Create the Facade that orchestrates the subsystems

class Facade:
    def __init__(
        self,
        a: SubsystemA | None = None,
        b: SubsystemB | None = None,
    ) -> None:
        self._a = a or SubsystemA()
        self._b = b or SubsystemB()

    def simple_operation(self) -> str:
        return "\n".join([
            "Facade initialises subsystems:",
            self._a.operation_a(),
            self._b.operation_b(),
            "Facade triggers subsystems:",
            self._a.operation_a2(),
            self._b.operation_b2(),
        ])

3Step 3: Use the simplified Facade interface

def client_code(facade: Facade) -> None:
    print(facade.simple_operation())


client_code(Facade())

Facade Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Facade Pattern in the Real World

A hotel concierge is a facade for the city’s complex infrastructure. Instead of you directly calling a taxi company, booking a restaurant, and arranging a museum ticket separately, the concierge handles all of it through a single conversation. The underlying services still exist in their full complexity—you just don’t deal with them directly.