Course contentsShow
← Software Engineering
Lesson 256 of 1,9338. Structural Design PatternsFree lesson

The Adapter Pattern: Connecting Incompatible Interfaces

Learn how the Adapter pattern wraps an existing class to match a required interface without modifying its source code.

The Adapter Pattern: Connecting Incompatible Interfaces

What you'll learn: How to make incompatible interfaces work together without changing their original code.

The Problem of Incompatible Interfaces

Imagine you have a European plug and you're visiting the United States. Your plug has two round prongs, but the American outlet expects two flat prongs. You need an adapter — a device that sits between your plug and the outlet, translating the interface without changing either the plug or the outlet itself.

In software, the Adapter pattern solves the same problem: you have an existing class with one interface, but your code expects a different interface. Rather than modifying the existing class (which might be third-party code, a legacy system, or simply violates the Open/Closed Principle), you create an adapter that wraps it.

How the Adapter Pattern Works

The Adapter pattern has three key parts:

  1. The Target Interface: What your code expects to work with
  2. The Adaptee: The existing class with an incompatible interface
  3. The Adapter: A wrapper that implements the target interface and translates calls to the adaptee

The adapter "speaks both languages" — it implements the interface your code needs while internally delegating work to the adaptee using its original interface.

When to Use It

Apply the Adapter pattern when:

  • You want to use an existing class, but its interface doesn't match what you need
  • You need to create a reusable class that cooperates with unrelated classes
  • You're working with third-party libraries you can't modify
  • You want to decouple your code from specific implementations (following Dependency Inversion Principle)

Key Takeaway: The Adapter pattern lets incompatible interfaces work together by wrapping one class to match another's expected interface, preserving the Open/Closed Principle and enabling code reuse without modification.