python-design-pattern-rag

The Adapter Pattern (Structural)

Purpose

The Adapter pattern allows objects with incompatible interfaces to work together. It acts as a bridge between an existing class and the interface your code expects. Rather than modifying existing code, you write a separate adapter that converts one interface to another.

Problem It Solves

Sometimes you want to use a class, but its methods don’t match what your code expects. This often happens with legacy code or third-party libraries. Changing the original class might be risky, impractical, or violate good design principles like the Single Responsibility Principle. The Adapter pattern lets you create a wrapper that makes the class compatible without altering its original code.

When to Use It

Use the Adapter pattern when:

When Not to Use It

Avoid this pattern if:

How It Works

You create a new class—the adapter—that wraps the existing class. This adapter implements the interface your code expects and translates calls or data to match the behavior of the wrapped class.

Real-World Analogy

Think of using a travel plug adapter. If your European charger doesn’t fit a U.S. outlet, the adapter converts the plug shape so your device works without needing to redesign the charger. In code, adapters serve the same purpose: making two mismatched pieces work together.

Simplified Example

Here’s a basic Python example:

# The interface expected by the client
class Target:
    def request(self):
        return "Target: The default behavior."

# An existing class with a different interface
class Adaptee:
    def specific_request(self):
        return ".eetpadA eht fo roivaheb laicepS"  # reversed string

# Adapter converts the interface of Adaptee to match Target
class Adapter(Target):
    def __init__(self, adaptee: Adaptee):
        self.adaptee = adaptee

    def request(self):
        return f"Adapter: (TRANSLATED) {self.adaptee.specific_request()[::-1]}"

Usage

adaptee = Adaptee()
adapter = Adapter(adaptee)
print(adapter.request())  # Output: Adapter: (TRANSLATED) Special behavior of the Adaptee.

In this example:

Learn More

See the complete Python example here: Adapter Pattern on GitHub