In the real-world, Adapter allows two incompatible interfaces to work together. Interfaces may be incompatible, but the inner functionality should suit the need.
The adapter pattern is a software design pattern that allows incompatible classes to work together by converting the interface of one class into an interface expected by the clients. It is often used to make existing classes work with others without modifying their source code. This pattern is also known as Wrapper.
Participants
The participant classes and objects are,
Target Interface
This is the domain-specific used by the clients.
Adapter class
A wrapper class implements the target interface and modifies Adaptee to the Target interface.
Adaptee class
The class which is used by the Adapter to reuse the existing functionality and modifies them for the desired use.
Client
This class will interact with the Adapter class using Target interface.
Real life example
Mobile battery needs 5V to charge but the normal socket produces either 220V (India) or 110V~120V (USA). To provide the Targeted power (5V), we use the Mobile charger to charge the battery. Mobile charger works as an adapter between mobile charging socket and the wall socket.
The Adapter design pattern solves problems like,
public class Volt {
private int volts;
public Volt(int v) {
this.volts = v;
}
public int getVolts() {
return volts;
}
public void setVolts(int volts) {
this.volts = volts;
}
}
/**
* Common interface.
*/
public interface ISocketAdapter {
public Volt getVolt();
}
/**
* This is the Adaptee class.
*/
public class WallSocketAdaptee {
public Volt getVolt() {
return new Volt(120);
}
}
/**
* Adapter class which has is working as converter.
*/
public class SocketClassAdapterImpl extends WallSocketAdaptee implements ISocketAdapter {
@Override
public Volt getVolt() {
Volt v = getVolt();
return convertVolt(v, 24);
}
private Volt convertVolt(Volt v, int i) {
return new Volt(v.getVolts() / i);
}
}
public class AdapterClient {
/** Volts from Adapter. */
private static void testAdapter() {
ISocketAdapter sockAdapter = new SocketClassAdapterImpl();
Volt volt = sockAdapter.getVolt();
System.out.println("Adapter producing " + volt.getVolts());
}
/** Driver method. */
public static void main(String[] args) {
testAdapter();
}
}
The Adapter design pattern solves problems like,
1. How can a class be reused that does not have an interface that a client requires?
2. How can classes that have incompatible interfaces work together?
3. How can an alternative interface be provided for a class?
Often an (already existing) class can't be reused only because its interface doesn't conform to the interface clients require.
The Adapter design pattern describes how to solve such problems
1. Define a separate Adapter class that converts the (incompatible) interface of a class (Adaptee) into another interface (Target) clients require.
2. Work through an Adapter to work with (reuse) classes that do not have the required interface.
The key idea in this pattern is to work through a separate Adapter that adapts the interface of an (already existing) class without changing it. Clients don't know whether they work with a Target class directly or through an Adapter with a class that does not have the Target interface.
Adapter pattern use in the Java Libraries
java.util.Arrays#asList().
java.util.Collections#list().
java.util.Collections#enumeration().
java.io.InputStreamReader(InputStream) (returns a Reader).
java.io.OutputStreamWriter(OutputStream) (returns a Writer).
javax.xml.bind.annotation.adapters.XmlAdapter#marshal() and #unmarshal().