The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

Adapter Design Pattern

Structural pattern. Structural design patterns are concerned with composing classes and objects into larger structures.

Intent: "Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces." [GoF]

Open in the interactive referenceDownload the PDF reference

Intent

The intent of the Adapter design pattern is to:
"Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of
incompatible interfaces."
[GoF]
See Problem and Solution sections for a more structured description of the intent.

  • The Adapter design pattern solves problems like:
    • How can a class be reused that has not the interface clients require?
    • How can classes work together that have incompatible interfaces?
  • Often a class (Adaptee) can not be reused only because its interface doesn't conform to the interface (Target) a client requires.
  • The Adapter pattern describes how to solve such problems:
    • Convert the interface of a class into another interface clients expect.
      Define a separate Adapter class that converts the interface of a class (Adaptee) into another interface (Target) clients require.
    • Clients that require a Target interface can work through an Adapter to work with classes that have an incompatible (not the Target) interface.

Problem

The Adapter design pattern solves problems like:
How can a class be reused that has not the interface clients require? How can classes work together that have incompatible interfaces?
See Applicability section for all problems Adapter can solve. See Solution section for how Adapter solves the problems.

  • Often a class (Adaptee) can not be reused only because its interface doesn't conform to the interface (Target) a client requires.
  • An inflexible way to solve this problem is to change the class so that its interface conforms to the required interface.
    But it's impossible to change a class each time another interface is needed.
  • That's the kind of approach to avoid if we want to reuse a class (Adaptee) that has an incompatible interface independently from (without having to change) the class.
  • In the Strategy design pattern, for example, clients require (depend on) a Strategy interface for performing an algorithm.
    To perform a specific algorithm, it should be possible that clients can work with a class that has not the Strategy interface (see Sample Code / Example 2).
  • For example, designing reusable classes.
    To make a class more reusable, its interface should be adaptable so that clients that want to reuse the class can specify the interface they require (built-in interface adaptation).

Solution

The Adapter design pattern provides a solution:
Define a separate Adapter class that converts the interface of a class (Adaptee)
into another interface (Target) clients require.
Work through an Adapter to work with classes that have not the required interface.
Describing the Adapter in more detail is the theme of the following sections.
See Applicability section for all problems Adapter can solve.

  • The key idea in this pattern (object version) is to work through a separate Adapter object that adapts the interface of an (already existing) object.
    Clients do not know whether they are working with a Target object or an Adapter.
  • There are two ways to define an Adapter:
    • Class Adapter: Uses inheritance to implement a Target interface in terms of (by inheriting from) an Adaptee class.
    • Object Adapter: Uses object composition to implement a Target interface in terms of (by delegating to) an Adaptee object (adaptee.specificOperation()).
    • A class adapter commits to (inherits from) an Adaptee class at compile-time. An object adapter is more flexible because it commits (delegates) to an Adaptee object at run-time (see also Sample Code / Example 1).
    • There exists a wide range of possible adaptations, from simply changing the
      name of an operation to supporting an entirely different set of operations.
  • Built-in interface adaptation.
    To make a class (Adaptee) more reusable, it can be configured with an adapter object that converts the interface of the class into the interface clients (that want to reuse the class) require.

Applicability

Design Problems

  • Class Adapter (Compile-Time Adaptation)
    • How can a class be reused that has not the interface clients require?
    • How can classes work together that have incompatible interfaces?
    • How can an alternative interface be provided for a class?
  • Object Adapter (Run-Time Adaptation)
    • How can an object be reused that has not the interface clients require?
    • How can objects work together that have incompatible interfaces?
    • How can an alternative interface be provided for an object?

Structure

Static Class Structure

  • Client
    • Refers to (depends on) the Target interface.
  • Target
    • Defines an interface the Client class requires.
  • Adapter
    • Implements the Target interface in terms of Adaptee.
  • Adaptee
    • Defines a class that gets adapted.

Dynamic Object Collaboration

  • In this sample scenario, a Client object works with a Target object directly and through an adapter (of type Target) with an Adaptee object.
  • The interaction starts with the Client object that calls operation() on a Target object, which performs the request and returns to the Client.
  • Thereafter, the Client calls operation() on an adapter object (of type Target).
  • adapter calls specificOperation() on an Adaptee object, which performs the request and returns to the adapter, which in turn returns to the Client.
  • adapter can do work of its own before and/or after forwarding a request to Adaptee.
  • See also Sample Code.

Implementation

Implementation Issues

  • Implementation Variants
    • The implementation of an adapter depends on how different Adaptee and Target interfaces are.
      "There is a spectrum of possible work, from simple interface conversion—for example, changing the names of operations—to supporting an entirely different set of operations." [GoF, p142]
    • An adapter can implement additional functionality that the adapted class doesn't provide but the clients need.

Sample Code 1

Basic Java code for implementing the sample UML diagrams.

 1  package com.sample.adapter.basic;
 2  public class Client { 
 3      // Running the Client class as application.
 4      public static void main(String[] args) { 
 5          // Creating an object adapter 
 6          // and configuring it with an Adaptee object.
 7          Target objectAdapter = new ObjectAdapter(new Adaptee());  
 8          System.out.println("(1) Object Adapter: " + objectAdapter.operation()); 
 9          
10          // Creating a class adapter 
11          // that commits to the Adaptee class at compile-time.
12          Target classAdapter = new ClassAdapterAdaptee();        
13          System.out.println("(2) Class Adapter : " + classAdapter.operation()); 
14      } 
15  } 
(1) Object Adapter: Hello World from Adaptee!
(2) Class Adapter : Hello World from Adaptee!

 1  package com.sample.adapter.basic;
 2  public interface Target { 
 3      String operation();
 4  } 

 1  package com.sample.adapter.basic;
 2  public class ObjectAdapter implements Target { 
 3      private Adaptee adaptee;
 4      
 5      public ObjectAdapter(Adaptee adaptee) { 
 6          this.adaptee = adaptee;
 7      } 
 8      public String operation() { 
 9          // Implementing the Target interface in terms of 
10          // (by delegating to) an Adaptee object.
11          return adaptee.specificOperation();
12      } 
13  } 

 1  package com.sample.adapter.basic;
 2  public class ClassAdapterAdaptee extends Adaptee implements Target { 
 3      public String operation() { 
 4          // Implementing the Target interface in terms of 
 5          // (by inheriting from) the Adaptee class.
 6          return specificOperation();
 7      } 
 8  } 

 1  package com.sample.adapter.basic;
 2  public class Adaptee { 
 3      public String specificOperation() { 
 4          return "Hello World from Adaptee!";
 5      } 
 6  } 

Not included on this page: Motivation, Consequences. See the GoF Design Patterns Reference (PDF) for the complete description.