cconnected.
Understand the pattern. Find your own way.Find your starting point
Your learning feed/Idea 159 of 218
Ideas this builds on
Low-level design159 / 218

Adapter: translate a boundary

Picture

Our app calls send(message), but an old client exposes deliver(text, priority). Neither side needs a rewrite.

Predict

Does an adapter need to replace the legacy implementation?

Walk through the small example

No, it delegates to it. It translates the boundary and reuses the existing implementation.

Mental model and complete Python solution

SenderAdapter owns a legacy collaborator and exposes the caller’s send method. Its translation sets priority to normal explicitly. The old client remains testable independently. An adapter reconciles incompatible existing interfaces; a bridge is designed to let two dimensions vary, while a decorator preserves an interface and adds behavior.

What the function promises: The local legacy client records deliveries. The adapter preserves exceptions from deliver; it does not perform real network delivery.

class LegacyClient:
    def __init__(self):
        self.deliveries = []
    def deliver(self, text, priority):
        self.deliveries.append((text, priority))

class SenderAdapter:
    def __init__(self, legacy):
        self.legacy = legacy
    def send(self, message):
        self.legacy.deliver(message, priority="normal")

legacy = LegacyClient()
SenderAdapter(legacy).send("hello")
assert legacy.deliveries == [("hello", "normal")]

Work and storage: O(1) wrapper work beyond the delegated operation.

When to recognize it: Choose a documented priority and preserve failures. Renaming a method is not enough if success means something different.

Check before applying it: An adapter must preserve behavior and errors, not only rename operations.

Change one rule

The legacy helper reports errors with a return code instead of exceptions. What should the adapter do?

See what changes

Translate that error convention into the interface promised to the caller. Do not silently report success after a failed legacy operation.

Connect

Agree on a small promise: Keep the caller’s small send promise while translating the legacy API.

Decorator: wrap one extra behavior: Compare translating an interface with wrapping extra behavior around the same interface.