Friday, February 5, 2016

Design Patterns : Adapter Pattern - Be Adaptive

You may also like to see:

Adapter pattern works as a bridge between two separate objects. It converts interface of a class into another interface which is required. Adapter pattern lets different classes work together that could not otherwise due to different interface of each class.

Understanding Adapter pattern is not difficult because it work same as we see adapter in real world. Simplest example is AC power adapter which we use for cell-phone, laptop chargers. Electricity at home socket is 220 volts (or 110 volts) but our cell phone needs 5-12 volts so we need an adapter which should converts the power to required range.

Adapter in Programming


Lets see a real world example for Adapter pattern. Information sharing system's team is working on a system which uses social networks sites to share the content. Team has designed an interface which is implemented by Facebook, Twitter and Linkedin classes. System is using this interface to switch between these sites and share the content. Developers have spend a lot of time in implementing these network site's public APIs in these classes and make system bug free and tested.


Here is code implementation of the system.
public interface ISocialNetworks
{
    bool Login(string username, string password);
    bool Share(string content);
}

public class Facebook : ISocialNetworks
{    
    public bool Login(string username, string password)
    {
       //Login Using Facebook API
    }

    public bool Share(string content)
    {
       //Share Using Facebook API
    }
}

public class Twitter : ISocialNetworks
{    
    public bool Login(string username, string password)
    {
       //Login Using Twitter API
    }

    public bool Share(string content)
    {
       //Share Using Twitter API
    }
}

//similarly for Linkedin


Everything was working fine until client wanted Google+ also in list and team know to implement the same interface you have to understand APIs and have to put huge effort in implementations. But you found that Google + has already developed a library so you downloaded that library and try to integrate in your system.


There is a little problem Google+ developers have implemented their own interface which is not compatible with information sharing system. Here is code:
public interface IGoogleSocialNetworks
{
    bool Authenticate(string username, string password);
    bool Post(string content);
}

public class GooglePlus : IGoogleSocialNetworks
{    
    public bool Authenticate(string username, string password)
    {
       //Login Using Google API
    }

    public bool Post(string content)
    {
       //Share Using Google API
    }
}


Now here comes the adapter which will convert interface of Google+ library to the interface of desired system interface i.e, ISocialNetworks. Lets see its code implementation
public class GoogleNetworksAdapter : ISocialNetworks
{
    GooglePlus GoogleLibrary;

    public void GoogleNetworksAdapter (GooglePlus googleLibrary)
    {
       this.GoogleLibrary = googleLibrary;
    }

    public bool Login(string username, string password)
    {
       GoogleLibrary.Authenticate(username, password);
    }

    public bool Share(string content)
    {
       GoogleLibrary.Post(content);
    }
}

Let see design diagram after implementing adapter pattern



Now since Google+ adapter has the same interface as our other Social Networks i.e, ISocialNetworks so it can be use in system easily. Here is how client uses the adapter:
  1.  Client make a request to Adapter class (GoogleAdapter) for a method of  target interface (ISocialNetworks) e.g, Login
  2. Adapter class translate that request to adaptee object method (IGoogleSocialNetwoks) e,g, Authenticate and return response
  3. Client receive the response without realizing that Adapter converted its request to another form.

Here is class diagram of Adapter Pattern


Adapter pattern use other good practices of Object Oriented for example object composition in Adapter class to wrap adaptee functionalities in target interface. Another advantage to use this object composition is that this adapter can be use with any child class that inherits this Adaptee class.

Special Scenario


We have seen a simple example in which all the methods of target interface were available in adaptee class. What if there is a method in target interface which is not available in adaptee in that case adapter pattern is not perfect to use because we have to throw a NotImplementedException, so client will have to watch out for possible exceptions from adapter. Adapter should be well documented to reduce the chance of leaving unhandled exception at client end.

1 comment:

Life insurance policy, bank loans, software, microsoft, facebook,mortgage,policy,