skip to content

Adapter Pattern - Make One Interface Look Like Another

Updated: at 12:00 AM

1. The Problem

Problem: Incompatible Interfaces Need to Work Together

Software often needs to integrate external systems, legacy components, or third-party libraries whose APIs do not match your system’s expectations.

Example: Your app expects an object with send(data) but a library exposes publish(payload).

You end up writing procedural glue everywhere:

if service == "legacy":
    legacy.publish(data)
else:
    modern.send(data)

This leads to:

You need a way to make incompatible interfaces compatible without modifying them.

2. The Adapter Pattern: Make One Interface Look Like Another

The Adapter Pattern allows objects with incompatible interfaces to collaborate.

Core idea:

A system using Adapter has:

You get:

Use Adapter when:

3. Implementation: Adapter in Python

Assume your application expects a Notifier with send(message).

Target interface:

from abc import ABC, abstractmethod

class Notifier(ABC):
    @abstractmethod
    def send(self, message: str):
        pass

Your modern service already matches it:

class EmailNotifier(Notifier):
    def send(self, message: str):
        return f"Email sent: {message}"

But a legacy library exposes a completely different API:

class LegacyAlertSystem:
    def publish_alert(self, payload: str):
        return f"Legacy alert published: {payload}"

Your system expects send(), but the legacy system uses publish_alert().

Adapter wraps the legacy system and adapts the API:

class LegacyAlertAdapter(Notifier):
    def __init__(self, legacy_system: LegacyAlertSystem):
        self.legacy = legacy_system

    def send(self, message: str):
        return self.legacy.publish_alert(message)

Client code works without knowing anything changed:

def notify_user(notifier: Notifier, message: str):
    print(notifier.send(message))

notify_user(EmailNotifier(), "Welcome!")
notify_user(LegacyAlertAdapter(LegacyAlertSystem()), "System Down!")

Output conceptually behaves the same from the client’s perspective.

4. When to Use Adapter (and When Not To)

You likely need Adapter when you notice:

Common pitfalls: