1. Home
  2. Explore
  3. Computer Science
  4. Adapter Design Pattern

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Adapter Design Pattern

Adapter Design Pattern

đŸ§© Overview: Four Easily Confused Patterns

The Adapter pattern is one of four structural design patterns that are often mistaken for one another. These four patterns—Adapter, Facade, Proxy, and Decorator—share structural similarities but serve distinct purposes. A quick reference for keeping them apart:

Adapter (Wrapper)

  • Purpose: Makes two incompatible interfaces compatible by wrapping one object so that it can be used by a client expecting a different interface.

  • Key idea: Interface conversion — adapts an existing class to a target interface without changing the existing class.

Facade

  • Purpose: Provides a simplified, unified interface to a set of interfaces in a subsystem.

  • Key idea: Hides complexity — the client interacts with a single facade instead of multiple complex objects.

Proxy

  • Purpose: Acts as a substitute or placeholder for another object to control access to it.

  • Key idea: Interception and control — the proxy sits between the client and the real object, adding security, caching, lazy loading, etc.

Decorator

  • Purpose: Adds new behavior to an existing object dynamically without altering its structure.

  • Key idea: Wrapper that adds responsibilities — you wrap the original object with one or more decorators, each adding a piece of functionality.

Pattern

Core Function

Common Synonym

Adapter

Converts one interface to another

Wrapper

Facade

Simplifies a complex subsystem

–

Proxy

Controls access to another object

Surrogate

Decorator

Adds behavior without modifying code

–


Note: The Decorator pattern was covered in a previous video. This set of notes focuses entirely on the Adapter pattern (also called a Wrapper).


🔌 The Adapter Pattern (Wrapper) — Deep Dive

What It Solves

In software development, we often encounter situations where classes or modules were designed independently and their interfaces don’t match. Rather than rewriting the existing code (which may be tested, stable, or from a third party), we can use an adapter to bridge the gap.

  • Problem: Two interfaces are incompatible — the client expects one method signature, but the available class has a different one.

  • Solution: Create an adapter class that implements the target interface and internally calls the methods of the adaptee (the existing class), translating the call as needed.

Real-World Analogy

Imagine you have a power plug from Europe (two round prongs) and you need to plug it into a US wall socket (two flat slots). You use a travel adapter — it has a US‑style plug on one end and a European‑style socket on the other. The adapter converts the interface without changing the device itself.

In software, the adapter does exactly that: it wraps an object (the adaptee) and presents a different interface to the client.

Structural Overview

The adapter pattern involves four key participants:

  1. Target — The interface that the client expects to work with.

  2. Client — The class that uses the target interface.

  3. Adaptee — The existing class with an incompatible interface.

  4. Adapter — The class that implements the target interface and delegates calls to the adaptee.

Client → [Target Interface] ← Adapter → Adaptee
  • The Adapter translates the target’s method calls into adaptee‑specific calls, often performing necessary conversions (data types, parameter order, etc.).

Key Characteristics

  • Also known as: Wrapper (it “wraps” the adaptee).

  • Purpose: Interface compatibility, not behavior addition.

  • How it differs from Decorator: A decorator adds new behavior; an adapter translates existing behavior to a different interface. An adapter is not meant to extend functionality.

Two Variants of Adapter

Variant

Description

Object Adapter

Uses composition: the adapter holds a reference to the adaptee object and delegates to it. Preferred because it works with any subclass of the adaptee.

Class Adapter

Uses inheritance: the adapter inherits from both the target interface and the adaptee. Only available in languages supporting multiple inheritance (e.g., C++). Less flexible.

Recommended approach: Object Adapter (composition over inheritance). It is more flexible and adheres to the principle of “favor composition over inheritance.”

Example (Conceptual)

Suppose you have a legacy Logger class that writes logs to a file with a method logMessage(String text). But your new system expects a LoggerInterface with a method log(String message, int level). You cannot modify the legacy Logger.

Solution: Create a LoggerAdapter that implements LoggerInterface:

  • log(String message, int level) internally calls logger.logMessage( message + " [level: " + level + "]" ).

The client now uses the adapter as if it were any other LoggerInterface implementation.

When to Use the Adapter Pattern

  • You want to use an existing class, but its interface doesn’t match the one you need.

  • You want to create a reusable class that cooperates with classes that don’t necessarily have compatible interfaces.

  • You need to integrate third‑party libraries or legacy code into your system without modifying them.

Benefits and Trade-offs

  • ✅ Benefits:

    • Single Responsibility Principle: separates interface conversion from business logic.

    • Open/Closed Principle: you can introduce new adapters without altering existing client code.

    • Reusability: existing classes can be reused in new contexts.

  • ⚠ Trade-offs:

    • Adds an extra layer of indirection, which may slightly affect performance.

    • If overused, the system can become cluttered with many adapter classes.


🔍 Recap: Adapter vs. Facade vs. Proxy vs. Decorator

Pattern

What It Does

Analogy

Adapter

Converts one interface to another

Travel power adapter

Facade

Simplifies a complex subsystem into a single easy‑to‑use interface

Reception desk at a hotel

Proxy

Controls access to an object (security, caching, lazy loading)

Security guard at a building

Decorator

Adds new behavior to an object without changing its code

Wrapping a gift with extra ribbons

Each pattern wraps something, but the intent is different. Adapter is about compatibility; Facade is about simplification; Proxy is about control; Decorator is about enhancement.


📘 Final Takeaway

The Adapter pattern (or Wrapper) is a fundamental tool for making incompatible interfaces work together. By wrapping an existing object in a new interface, it promotes code reuse and keeps systems decoupled. It is one of the most frequently used patterns, especially when working with legacy code, third‑party libraries, or APIs that don’t quite fit.

Key definition: An adapter converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent