In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
An example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.
The adapter design pattern is one of the twenty-three well-known Gang of Four design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
The adapter design pattern solves problems like:
Often an (already existing) class can not be reused only because its interface does not conform to the interface clients require.
The adapter design pattern describes how to solve such problems:
The key idea in this pattern is to work through a separate <code>adapter</code> that adapts the interface of an (already existing) class without changing it.
Clients don't know whether they work with a <code>target</code> class directly or through an <code>adapter</code> with a class that does not have the <code>target</code> interface.
See also the UML class diagram below.
An adapter allows two incompatible interfaces to work together. This is the real-world definition for an adapter. Interfaces may be incompatible, but the inner functionality should suit the need. The adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.
An adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior. Alternatively, a decorator pattern makes it possible to add or alter behavior of an interface at run-time, and a facade pattern is used when an easier or simpler interface to an underlying object is desired.
In the above UML class diagram, the <code>client</code> class that requires a <code>target</code> interface cannot reuse the <code>adaptee</code> class directly because its interface doesn't conform to the <code>target</code> interface. Instead, the <code>client</code> works through an <code>adapter</code> class that implements the <code>target</code> interface in terms of <code>adaptee</code>:
In this adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.
This adapter pattern uses multiple polymorphic interfaces implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java (before JDK 1.8) that do not support multiple inheritance of classes.
It is desired for to supply with some data, let us suppose some data. A compile time solution is:
However, suppose that the format of the string data must be varied. A compile time solution is to use inheritance:
and perhaps create the correctly "formatting" object at runtime by means of the factory pattern.
A solution using "adapters" proceeds as follows:
When implementing the adapter pattern, for clarity, one can apply the class name to the provider implementation; for example, . It should have a constructor method with an adaptee class variable as a parameter. This parameter will be passed to an instance member of . When the clientMethod is called, it will have access to the adaptee instance that allows for accessing the required data of the adaptee and performing operations on that data that generates the desired output.
Output <pre> Recharging android with MicroUsb MicroUsb connected Recharge started Recharge finished Recharging iPhone with Lightning Lightning connected Recharge started Recharge finished Recharging iPhone with MicroUsb MicroUsb connected Lightning connected Recharge started Recharge finished </pre>
Output: