Adapter design pattern using C#

Moving on to the concept of the design patterns, let’s discuss the Adapter design pattern. This pattern is part of the structural design patterns.

What is Adapter design pattern ?

According to GoF’s definition, this pattern is :

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

To understand this definition, let’s take a simple example from a real world. You know a language A. You visit a place where language B is spoken. But you don’t know how to speak that language. So you meet a person who knows both languages i.e. A and B. So he can act as an adapter for you to talk with the local people of that place and help you communicate with them.

Another example, a kind of situation, where the client hires someone to perform a task for him, which he cannot do it directly with the third person, This means, that the client needs an intermediate to co-ordinate with the third person and get the task done for him. This is what the role of a adapter is.

To start with, this pattern can be implemented in two different ways (or 2 types you can say)  We will be discussing both of them along with a real world example for better understanding. But before that, we will define the components of our system. They include :

1. Adaptee – Defines an existing interface that needs adapting  : It represents the component with which the client wants to interact with the.  

2. Target – Defines the domain-specific interface that Client uses : It basically represents the interface of the adapter that helps the client interact with the adaptee.

3. Adapter – Adapts the interface Adaptee to the Target interface  : It means, it implements the Target interface, defined above and connects the adaptee, with the client, using the target interface implmentation

4. Client – The main client that wants to get the operation, get done from the Adaptee.

Now moving on to the types and their implementations

Class Adapter

A real world example :

You work on a client’s projects. You have team of two developers A & B (specialist in ASP.NET), who work on this client’s project directly, without your involvement. This is important to note here i.e. without your involvement. We will come to this why it is so  ?

So technically your code will be like the one below :


Everything is fine till now. So your client has a new assignment and needs a Silverlight technology to work on. You don’t have the team for this technology and don’t want to loose the opportunity to work on it So what you do ? You create a new team and add members to it.

Since it is a new technology and you have a good hand on the silverlight, you decide to monitor the things yourself and get the task done. So this is where the role of an adapter comes into the picture. You become an adapter and get the things done, by being an intermediate for the client and the new silverlight team. You get the requirement from the client and pass them to team and execute the project. So here our adapter is now represented by the ExecuteProject() method in the adapter class. See the code below :


This is why I said without your involvement, when i started with the concept. The main requirement that forced us to use an adapter is that client is not compatible with the new team introduced by you.So you represent yourself to the client, on behalf of your team and get the project done. Here, you bring your team into the picture using the concept of inheritance and in our code, the respresentations are :

Adaptee is SilverlightTeamAdded class

Target is represented by ITeams

Adapter is represented by the AdapterClass

So this is the way of implementing the Class adapter. Now moving on to the Object adapter.

Object Adapter :

Now since you have completed the first silverlight project of the client successfully, he is very happy with your performance. So he wants you to handle another project of his. But, now he is quite comfortable with the team, he is ready to deal directly with your team (but have you in the picture of-course). So this time, you get the requirements from the client and delegate the responsibility directly to the team. So this is where the basic difference between these two types comes. We will discuss the detailed difference, after the article. So our code implementation needs little change and becomes the following :


Rest of the code remains the same. You might think that the logically speaking, both the projects are being delegated to the base silverlight team itself, for completion. Yes, its correct that both the projects are being executed by the silverlight team. Then, next question is what is the difference between the two implementations then ? This is what we will discuss next.

Class Adapter vs Object Adapters

The most important difference between is that Class adapter uses the concept of the Inheritance whereas, object adapter uses the concept of Composition. 

So when you handled the first project, your team worked under you as an inherited team. In next project, you acted just a cover member, and delegated the responsibility to the team, using the concept of the composition.

Just a brief discussion, Inheritance and Composition, are very important concepts of object oriented programming and understanding these is very important, if we really want to use appropriate adapter in our code. Otherwise, we may try to implement the  Class adapter, where object adapter can be easily used & vice-versa.

When to use Adapter Pattern : 

1. When you have a third party code which cannot interact with the client code. For example, you might want to use a third party logger service, but your code is having incompatibility issues, you can use this pattern

2. When you want to use an existing code with extended functionality but not without changing it, as it is being used in other components, you can extend it using the adapter pattern.

3. Again you can use object adapter for a code, which is using sealed class components, or needs multiple inheritance. For a requirement where you need to use single inheritance, you can choose class adapter.

So this was all about this design pattern.

Happy Coding…!!!


About Jasminder

.Net developer and blogger by profession, keen to learn new technologies, love nature, music and cricket.
This entry was posted in Design Patterns and tagged . Bookmark the permalink.

I have a suggestion..

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

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