Adapter Pattern

PUBLISHED: 2026-07-21
AUTHOR: MANUEL PRIETO
Design Patterns

Adapter (or Wrapper) is a structural design pattern that allows objects with incompatible interfaces to work together.

Purpose and Use Case

Use the Adapter pattern when you want to use an existing class or third-party library whose interface doesn't match the rest of your application code. It's ideal for keeping the Open/Closed Principle (SOLIDGlosarioSOLIDAcrónimo de cinco principios de diseño orientado a objetos y programación diseñados para hacer que el software sea más comprensible, flexible y mantenible: S - Single Responsibility Principle (Responsabilidad Única) O - Open/Closed Principle (Abierto/Cerrado) L - Liskov Substitution Principle (Sustitución de Liskov) I - Interface Segregation Principle (Segregación de Interfaz) D - Dependency Inversion Principle (Inversión de Dependencias) Ver término completo →) and avoiding duplicated code (DRYGlosarioDRY (Don't Repeat Yourself)Principio fundamental de diseño de software formulado por Andy Hunt y Dave Thomas. Establece que toda pieza de conocimiento o lógica debe tener una representación única, no ambigua y definitiva en el sistema para evitar duplicidad y facilitar el mantenimiento.Ver término completo →).

Unlike the Decorator Pattern (which adds new responsibilities), the Adapter solely translates an object's interface to match the client's expectations.

Pattern Structure

  1. Client: Contains the existing business logic of the application.
  2. Client Interface (Target): Declares the protocol that other classes must follow to collaborate with the client.
  3. Adaptee: Incompatible third-party class or service you want to use.
  4. Adapter: Wrapper class that implements the client interface and translates calls to the adaptee.
Cargando diagrama...

Execution Flow

  1. Invocation: Client calls a method on the target interface via the Adapter object.
  2. Translation: The Adapter converts received parameters to the format expected by the adaptee.
  3. Execution: The Adapter delegates the actual call to the Adaptee and returns the processed response to the client.

Real Implementation Examples