Design Pattern Part–3.


Adapter Pattern

Two different objects have unfriendly interfaces and then we introduce adapter to communicate and interact between them without any changes to the existing interface of the two objects.

Adapter converts the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

An Adapter could indeed have the same interface as the object which it adapts.  In that case, the adapter would add some extra functionality before making the call to the adaptee object.  So, we would not want the exact same interface.  Also, the adapter’s implementation would be different from that of the proxy.

Adapter pattern is like developing mobile software with lot of adapter to run the same software on different mobile brands. Every developer should write documentation about what that adapter method does it do? How to install and configure adapter?

There are four roles within the adapter pattern: client, adapter, adaptee, and the target interface.

The client is a user object that expects a specific interface (or type). We could for the purposes of simplicity state that the client is callee on a type that implements a specific interface. As another example the client may need to concert with a type later on that has implemented a specific interface.

The Adapter wraps the adaptee up and implements the interface the client expects. The adapter sometimes delegates its work to the adaptee.

An adaptee is the type that we want to use with the new code but we can’t because it doesn’t adhere to expected behavior that the client expects.

Finally the target interface is the name given to the interface that the client expects.

Let us illustrate the above mentioned  terminology with the help of a simple example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DesignPatterns.Adapter {
//Interface
public abstract class Connector     {
public abstract void ReadData();
}
//Adaptee
public class DatabaseUtil
{
      public void QueryForUpdates()
      {
          Console.WriteLine("Updates queried from DB.");
     }
}
//Adapter
public class DatabaseConnector : Connector
          {
          public override void ReadData()
         {
         var dbUtil = new DatabaseUtil();
         dbUtil.QueryForChanges();
         }
     }
//Client
     public class TradingDataImporter
     {
         public void ImportData(Connector connector)
         {
             connector.ReadData();
         }
     }
 }
UML Diagram of Adapter Design Pattern
UML Diagram of Adapter Design Pattern

To summarize, we have the following options when matching adapter and adaptee interfaces:

Adapter interface and adaptee interface have same signature
Many examples of the Adapter pattern operate at this level and are not illustrative or helpful in explaining its real power. Beware of them.
Adapter interface has fewer parameters than adaptee interface
The adapter calls the adaptee with some dummy input.
Adapter interface has more parameters than adaptee interface
The adapter adds the missing functionality, making it half an adapter and half a component in its own right.
Adapter interface has other types than adaptee interface
The adapter performs some type conversion (casting).

You should use Adapter pattern when you need to implement

  • A domain-specific interface.
  • A class to connect to with a mismatching interface.
  • a reusable class to cooperate with yet-to-be-built classes.
  • the modification of names of methods as called and as implemented.
  • different sets of methods for different purposes.

In the Next blog I will be explaining about the Bridge pattern. By that time read the blog and send me the feedback.

Digg This
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s