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 separateAdapterclass that converts the interface of a class (Adaptee) into another interface (Target) clients require. -
Clients that require a
Targetinterface can work through anAdapterto work with classes that have an incompatible (not theTarget) interface.
-
Convert the interface of
a class into another interface clients
expect.
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
Strategyinterface for performing an algorithm.
To perform a specific algorithm, it should be possible that clients can work with a class that has not theStrategyinterface (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
Adapterobject that adapts the interface of an (already existing) object.
Clients do not know whether they are working with aTargetobject or anAdapter. -
There are two ways to define an
Adapter:-
Class Adapter:
Uses inheritance to
implement a
Targetinterface in terms of (by inheriting from) anAdapteeclass. -
Object Adapter:
Uses object composition
to implement a
Targetinterface in terms of (by delegating to) anAdapteeobject (adaptee.specificOperation()). -
A class adapter
commits to (inherits from) an
Adapteeclass at compile-time. An object adapter is more flexible because it commits (delegates) to anAdapteeobject 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.
-
Class Adapter:
Uses inheritance to
implement a
-
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
Targetinterface.
-
Refers to (depends on)
the
-
Target-
Defines an interface
the
Clientclass requires.
-
Defines an interface
the
-
Adapter-
Implements the
Targetinterface in terms ofAdaptee.
-
Implements the
-
Adaptee- Defines a class that gets adapted.
Dynamic Object Collaboration
-
In this sample scenario,
a
Clientobject works with aTargetobject directly and through anadapter(of typeTarget) with anAdapteeobject. -
The interaction starts with
the
Clientobject that callsoperation()on aTargetobject, which performs the request and returns to theClient. -
Thereafter,
the
Clientcallsoperation()on anadapterobject (of typeTarget). -
adaptercallsspecificOperation()on anAdapteeobject, which performs the request and returns to theadapter, which in turn returns to theClient. -
adaptercan do work of its own before and/or after forwarding a request toAdaptee. -
See also Sample Code.
Implementation
Implementation Issues
-
Implementation Variants
- The implementation of an adapter depends on
how different
AdapteeandTargetinterfaces 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.
- The implementation of an adapter depends on
how different
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.