jobs4timesLogo jobs4timesLogo

Adaptor Design Pattern

  • Adaptor pattern helps you in converting interfaces of a class into another interface clients expects.
  • We can apply this to our real world examples also. The best example for this is an AC power adapter.
  • When we buy some imported items from other countries, they come with sockets of different model, which cannot be plugged into our plugs.
  • To make them compatible, we attach converts in between so that they can be plugged-in.

  • Let's take an example to understand better. We have an algorithm; it takes the name of the city and returns the temperature in the city.
  • But the user inputs the zipCode of the location he lives rather than the city name.
  • So to find the temperature our algorithm will not accept zipCode rather expects the city name.

In this case we can write an Adaptor class with takes the zipCode and maps to a city and passes this city name as input in finding the temperature at that location.

package design;

public interface IWeatherFinder {

 public int find(String city);
package design;

public class WeatherFinder implements IWeatherFinder{

 public int find(String city) {	 
  return 22;

The above class contains the logic for finding the temperature in a city.
Now my client instead of having name of the city, he has zipCode for which we need to find the temperature.

In this case the interface the client expected is different from the actual algorithm which has been designed.
To fix this problem we need to write one adaptor class.

package design;

public class WeatherAdaptor {

 public int findTemperature(int zipCode) {
	 //maps this zipCode to city name
	 String city=null;
	 //actually looks into a source (db or file)
   IWeatherFinder finder=new WeatherFinder();
   return finder.find(city);


Now the client can find the temperature by passing the zipCode,
where the zipCode will be mapped to the city by adaptor and talks to respective class to find the temperature.

package design;

public class WeatherWidget {

 public void showTemparature(int zipCode){
  WeatherAdaptor wa=new WeatherAdaptor();


Even adaptor pattern makes in-compatible interfaces compatible,
it introduces one more level of in-direction the code which makes it complicated to understand and sometimes tough to debug.