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:
Target â The interface that the client expects to work with.
Client â The class that uses the target interface.
Adaptee â The existing class with an incompatible interface.
Adapter â The class that implements the target interface and delegates calls to the adaptee.
Client â [Target Interface] â Adapter â AdapteeThe 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 callslogger.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.