Adapter Design Pattern.

Hi Guys,

I have recently started reading about Design Patterns in Java and J2EE. I knew about them but then I though I should try to learn them in depth and try to Implement them. So here come the very first one called Adapter Pattern.

Basically this is used to relate two things which are actually unrelated. Confused??

Ok!!  Let me clear this for you. A very simple example is using phone charger. Suppose your mobile phone needs 9 Volts of supply to get charged but main supply is 220 V which is not what you require but it can be a source and you can have someone who can get 9 V out of this 220 V and supply to your phone for charging.

Now phone charger is acting as an adapter for you.  So Basically it is making a relationship between two unrelated interfaces. This  is how Adpater pattern works.

Lets talk in programming language context now. For example I have a class ExceptionInformer *which has a method called _getExceptionMessageBasedOnExceptionType()_.  Now client has an interface *IClientInterface *which has an abstract method called _getCauseBehindException() *_which needs to be implemented for the same purpose as *ExceptionInformer *but method name is different now Client wants you to implement this method.

You know that you have something which can fullfill your purpose.  **According to the Adapter Pattern you can create a class which will implement IClientInterface and as well as extend ExceptionInformer . **Implement method from Interface and in the implementation call method from ExceptionInformer class.

I have implemented this using a different example not sure whether this is a perfect example for this pattern or not but yes I have kept it very simple and must clear the basic idea behind this pattern. Please see code below.

import java.util.Locale;  

/*  
 * This is an existing class that has a single method  
 * getCountryName which returns display name of the country  
 * based on the the defualt locale.  
 */  

  public class CountryInformer{  

 /*  
  * This method returns display name of the country  
  * based on default locale.  
  */  
  public String getCountryName(){  
        return Locale.getDefault().getDisplayCountry();  
  }  
}
/*  
 * This is the interface that client is using to get current  
 * location which is expected to be Country name of the user.  
 */  
  public interface ILocationLocator{  
 /*  
  * This method will return the current location based  
  * on the default locale.  
  */  
  public abstract String getCurrentLocation();  
}
/*  
 * This class acts as heart of Adapter pattern and will  
 * implement the interface provided by the Client and  
 * existing class that provides the required functionality.  
 */  
 public class LocationAdapter extends CountryInformer  
                         implements ILocationLocator{  
 /*  
  * @see ILocationLocator#getCurrentLocation()  
  */  
  public String getCurrentLocation(){  
   /*  
    * Here comes the adapter pattern we are using this  
    * adapter to use the existing functionality from  
    * CountryInformer class without informing the  
    * client so the CountryInformer  
    * class acts as a plugin.  
    */  
     return super.getCountryName();  
   }  
}
 /*  
  * This class is for testing Adapter Pattern.  
  */  
  public  class LocationAdapterTest{  
     public static void main(String[] argv){  
         /* Create object of adapter class */  
         ILocationLocator locator = new LocationAdapter();  
         System.out.println("Current Location of user is: "  
                             +locator.getCurrentLocation());  
     }  
 }

I hope this will help you to have basic understanding I will write about more Design patterns and also Implementation of
those design pattern keep reading my blog!

Share on : Twitter, Facebook or Google+