Skip to content

Lesson 9 of 9 · 25m

Structural Patterns: Adapter, Decorator, Facade — and Pattern Triage

The structural patterns that matter in Python, how decorators differ from the Decorator pattern, and a field guide for deciding whether a pattern clarifies or merely decorates your résumé.

Structural patterns are about fitting pieces together without letting every piece know every other piece. Python's version is pragmatic: adapters isolate third-party shapes, decorators add behavior transparently, facades narrow sprawling subsystems. The anti-pattern is using these names to make ordinary indirection sound sophisticated.

Adapter: translate at the boundary

class StripeCustomerAdapter:
    def __init__(self, customer):
        self.customer = customer
    @property
    def id(self) -> str:
        return self.customer["id"]
    @property
    def email(self) -> str:
        return self.customer.get("email", "")

Your application should not scatter customer["metadata"]["tier"] through forty modules. Adapt external shapes once, at the boundary, into your domain vocabulary. When the vendor changes response format, the blast radius is one adapter and its tests.

Decorator: same interface, added behavior

class CachedRepo:
    def __init__(self, inner, cache):
        self.inner = inner
        self.cache = cache
    def get_user(self, user_id):
        if user_id in self.cache:
            return self.cache[user_id]
        user = self.inner.get_user(user_id)
        self.cache[user_id] = user
        return user

CachedRepo implements the same repository interface as inner and adds caching. That is the Decorator pattern. Python's @decorator syntax is related but broader: function decorators can add logging, validation, retries, or registration without implementing the same interface. Do not confuse the mechanism with the pattern.

Facade: shrink a subsystem to the use case

class BackupService:
    def __init__(self, compressor, storage, manifest):
        self.compressor = compressor
        self.storage = storage
        self.manifest = manifest
    def backup_database(self, source):
        archive = self.compressor.pack(source)
        key = self.storage.put(archive)
        self.manifest.record(source, key)

The facade does not hide power because it is lazy; it hides coordination because callers should not reimplement sequencing, error handling, and compensating actions. The subsystem can still expose lower-level objects to the rare caller that needs them.

Pattern triage questions

Before applying a named pattern, ask: What variation am I isolating? What implementation detail am I preventing callers from learning? Can a function, module, dataclass, or type annotation express this more directly? Will a new hire understand the flow by reading one file? If the honest answers are vague, the pattern is likely ceremony.

Field notes

  • Adapters belong at system edges, not between every pair of internal objects.
  • Decorators that reorder operations or swallow errors must document that behavior; transparent wrappers cannot be invisible in a debugger and a mystery in production.
  • Facades should own workflows, not become god objects that import the entire subsystem.
  • The best pattern name is often no name: 'a function passed as an argument' is clearer than 'inverted strategy object.'

Was this lesson helpful?

Sign in to rate