Adapter pattern acts as a bridge for connecting the interfaces and is mainly used to link two independent interfaces. The object that is used for linking the interfaces is known as "Adapter". Adapter pattern is a sub category of structural pattern.
Description
Adapter class contains a single class that is responsible to combine the interfaces. Adapter patterns provide interfaces to the client as per the requirement while working with the services of a class. Adapter pattern is also known as "wrapper ".
Advantages
Combining two independent interfaces is easy.
Reusability of existing functionality can be achieved.
Converts interface of a class as per the client requirements.
Objects can make use of the existing class.
Classes can be used without changing the system.
Conceptual
figure
Target : Target is used to define the domain-specific interface that the client use.
Adapter : Adapter adapts the interface 'Adaptee' to the target interface.
Adaptee : Adaptee is used to define the existing interface that needs adapting.
Client : Client collaborates with objects conforming to the target interface.
Examples
Creating a class Current to measure the usage of current.
[java]
class Current
{
private int volt;
public Volt(int v)
{
this.volts=v;
}
public int getVolt()//returns type is int
{
return volt;
}
public void setVolts(int volt)
{
this.volt = volt;
}
}
class Source
{
Volt getVolt()
{
return new Volt(120);
}
}[/java]
Creating an interface SourceAdapter, which will produce 120, 12, 3 volts.
[java]
public interface SourceAdapter
{
Volt get120Volt();
Volt get12Volt();
Volt get3Volt();
}[/java]
Creating a SourceAdapter class, which will implement the SourceAdapter.
[java]
class SourceAdapterImpl extends Source implements SourceAdapter
{
Volt get120Volt()
{
return getVolt();
}
Volt get12Volt()
{
Volt v= getVolt();
return convertVolt(v,10);
}
Volt get3Volt()
{
Volt v= getVolt();
return convertVolt(v,40);
}
private Volt convertVolt(Volt v, int i)
{
return new Volt(v.getVolts()/i);
}
}[/java]
Creating a Class SourceObjectAdapter, which implements the SourceAdapter.
[java]
class SourceObjectAdapterImpl implements SourceAdapter
{
private Source sock = new Source();
Volt get120Volt()
{
return sock.getVolt();
}
Volt get12Volt()
{
Volt v= sock.getVolt();
return convertVolt(v,10);
}
Volt get3Volt()
{
Volt v= sock.getVolt();
return convertVolt(v,40);
}
private Volt convertVolt(Volt v, int i)
{
return new Volt(v.getVolts()/i);
}
}[/java]
Creating a Class AdapterPatternTest for implementing the SourceAdapter classes.
[java]
class AdapterPatternTest
{
public static void main(String[] args)
{
testClassAdapter();
testObjectAdapter();
}
private static void testObjectAdapter()
{
SourceAdapter sockAdapter = new SourceObjectAdapterImpl();
Volt v3 = getVolt(sockAdapter,30);
Volt v12 = getVolt(sockAdapter,40);
Volt v120 = getVolt(sockAdapter,150);
System.out.println("v3 volts using Object Adapter="+v30.getVolts());
System.out.println("v12 volts using Object Adapter="+v40.getVolts());
System.out.println("v120 volts using Object Adapter="+v150.getVolts());
}
private static void testClassAdapter()
{
SourceAdapter sockAdapter = new SourceClassAdapterImpl();
Volt v3 = getVolt(sockAdapter,30);
Volt v12 = getVolt(sockAdapter,40);
Volt v120 = getVolt(sockAdapter,150);
System.out.println("v3 volts using Class Adapter="+v30.getVolts());
System.out.println("v12 volts using Class Adapter="+v40.getVolts());
System.out.println("v120 volts using Class Adapter="+v150.getVolts());
}
private static Volt getVolt(SourceAdapter sockAdapter, int j)
{
switch (j)
{
case 30: return sockAdapter.get30Volt();
case 40: return sockAdapter.get40Volt();
case 150: return sockAdapter.get150Volt();
default: return sockAdapter.get120Volt();
}
}
}[/java]
Output
[java]v3 volts using Class Adapter=3
v12 volts using Class Adapter=30
v120 volts using Class Adapter=12
v3 volts using Object Adapter=40
v12 volts using Object Adapter=120
v120 volts using Object Adapter=150[/java]
Summary
Key Points
Adapter Pattern - The objects of adapter can be replaced in the run time because they implement the same interface.
Additional responsibilities can be added by an interface.