Design Patterns - SPLessons

Adapter Pattern

Chapter 10

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Adapter Pattern

Adapter Pattern

shape Introduction

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.

shape 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 “.

shape 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.

shape Conceptual
figure

shape Examples

Creating a class Current to measure the usage of current.

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);
}
}

Creating an interface SourceAdapter, which will produce 120, 12, 3 volts.

public interface SourceAdapter
{
Volt get120Volt();
Volt get12Volt();
Volt get3Volt();
}

Creating a SourceAdapter class, which will implement the SourceAdapter.

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);
}
}

Creating a Class SourceObjectAdapter, which implements the SourceAdapter.

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);
}
}

Creating a Class AdapterPatternTest for implementing the SourceAdapter classes.

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();
}
}
}

shape Output

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

Summary

shape 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.