C# · CodeProject · Dotnet

Structural Design Pattern (Part 3)

Dear Reader,

This is in continuation to the previous post. Please note that this post will be long one, so if you have time keep reading 🙂

Let me start off with the first pattern i picked up to talk about in Structural patterns category.

Adapter Pattern:


It states that “Translating one interface of a component or a class to a compatible interface”, it may not look simple as you read it. What it means is that many times in real world what happens is one component wont fit into another component appropriately. We often tend to overlook the compatibility issues and thus encounter problem while fitting them together to make it work.

If they are newly designed components, then it’s not a major problem for us to worry, because both of them will be designed as perfectly as specified to get coupled, but if one of the component is existing or generic and other is already designed or modification needed, then its a problem for us.


As said above, there are many places which we see in our real life examples where in many components wont fit to the existing components at all.

Assume there is a legacy driver component which your project would be interested in to use. But its interface wont match with yours, and neither you can change in your project to make it work due to design issues or cost of change is huge/complex.

Lets assume the workaround way where in we either did not know about this pattern nor did not want to use it. What would happen is, to fit component A and B together at position Y, due to incompatibility issues you either would change component A or B and make them fit together, but did not realize that either of these component is already broken due to this change or you have violated the design which it was built on.

Next to fit B and C component at X position again due to same reasons you would change B (assumed) and fit them all together, but if these chaining continues a lot more like this, think about the complexity you have added to the software module. After a while, the whole changes made looks so ugly for yourself or for the other developers looking at this components, also its very difficult to understand because what your design says is quite different than what your implementation says, And also to fix a bug or add feature to a component gets quite tedious job. Overall the complexity in maintaining this code part gets really complex in a long run.


As stated above, to make 2 components to co-work together perfectly we are in need of an interface which is compatible to both of the components.

So lets look at the code i wrote which simulates this pattern usage, of course it’s very much simplistic in nature.

  1. public interface IOldTarget
  2.     {
  3.         void Add(float a, float b);
  4.     }
  5.     public class OldTarget : IOldTarget
  6.     {
  7.         public void Add(float a, float b)
  8.         {
  9.             Console.WriteLine(“Adding 2 float values “ + (a + b));
  10.             //more code….
  11.         }
  12.     }

As you can see from above code, i here by have an Old target component which i am supposed to work with. As said i also have a new target component which is being developed but its interface wont fit with the Old target, lets look at it:

  1.  public interface INewTarget
  2.     {
  3.         void Add(int a, int b);
  4.     }
  5.     public class NewTarget : INewTarget
  6.     {
  7.         public void Add(int a, int b)
  8.         {
  9.             Console.WriteLine(“Adding integet values “+(a + b));
  10.             //More code goes here..
  11.         }
  12.     }

So now its pretty clear to you that the old and new targets wont directly fit at all. So what we need to do to solve this in-compatibility is to bring in Mr. Adapter. So lets invite him and see what good he can do:

  1.  public class TargetAdapter : IOldTarget
  2.     {
  3.         INewTarget newTarget;
  4.         public TargetAdapter(INewTarget target)
  5.         {
  6.             this.newTarget = target;
  7.         }
  8.         public void Add(float a, float b)
  9.         {
  10.             newTarget.Add((int)a, (int)b);
  11.         }
  12.     }

So what Mr. Adapter guy is doing here is, he is acting as a compatible interface between those 2 components. Now all of these component can work happily together in system. Lets see the usage code part of this adapter guy:

  1. static void Main(string[] args)
  2.         {
  3.             int a = 5 , b = 5;
  4.             float c = 5.2f, d = 5.2f;
  5.             INewTarget newTarget = new NewTarget();
  6.             IOldTarget oldTarget = new OldTarget();
  7.             newTarget.Add(a, b);
  8.                oldTarget.Add(c, d);
  9.             oldTarget = new TargetAdapter(newTarget);
  10.             oldTarget.Add(c, d);
  11.                Console.ReadLine();
  12.         }

The advantage here is, if by some reason one of the component interface gets changed or added, then other one need not to worry much, just the interfacing guy has to be aware of it. So the over all cost of change in the system is very much minimized by using this pattern.


Lets look at couple of real world examples where we do come across such pattern usage:

  1. A micro SD card surely wont fit into the laptops SD card slot. Instead of changing either of those components we make use of some adapter component as show in the above picture.
  2. There are many electrical components whose pins are in different shapes, but there are common slots in the walls where you can’t plug any shaped pins. So we make use of adapter plugs there too as shown in pic.
  3. To fit 2 different sized water pipes together, we make use of Adapter component.

Well, that’s all dear friend i can talk about this pattern. I am sorry if it’s too lengthy, but i wanted to make every point clear.

Look out for my next post in continuation to this.

Thanks 🙂

P.S: Your valuable comments, votes are well appreciated.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s