Click here to Skip to main content
Click here to Skip to main content

Adapter Design Pattern (C#)

, 16 May 2014
Rate this:
Please Sign up or sign in to vote.
The article explains Adapter pattern, implemented in C#.

Introduction

In this article I would try to explore a very useful and commonly used design pattern- "The Adapter Pattern". I will cite my examples using C# language.

Background

Adapter pattern is placed under the category of structural design pattern. “Adapter” as the name suggests is the object which lets two mutually incompatible interfaces communicate with each other. But why we want “someone” to adapt to something? You will get an answer if you answer this simple thing- Your laptop charger which you bought in US has flattish pins which easily gets hooked into electrical sockets in US. But when you travel to European countries you may have round holes in the electrical sockets. What do you do then?-Simple buy socket adapters/converters for that.

We use Adapters when incompatible interfaces are involved. Our client object wants to call a method but it is not able to because the interface which our client object can use, is not available with the code which our client object wants to call. Based on what an adapter does the adapter design pattern is also called wrapper pattern, translator pattern. Let’s look at the various participants (objects, interfaces) involved in an adapter pattern.

ITarget: This is the interface which is used by the client to achieve functionality.

Adaptee: This is the functionality which the client desires but its interface is not compatible with the client.
Client: This is the class which wants to achieve some functionality by using the adaptee’s code.
Adapter: This is the class which would implement ITarget and would call the Adaptee code which the client wants to call.

Using the code

Now let’s look at an example to see how adapter pattern works. Imagine an online shopping portal which displays the products for selling on its home page. These products are coming from a third party vendor with which the portal has tied hands to sell products. The third party vendor already has an inventory system in place which can give the list of products it is selling There is no interface available to online shopping portal with which the portal can call the third party vendor’s inventor code.

Since we want to call the third party vendor’s code which is incompatible with client (online shopping portal) code we can apply “Adapter design pattern” here. Let’s first fit in various participants of an adapter pattern in our scenario.
ITarget: Method which the online shopping portal calls to get the list of products. Here getting the list of products is the functionality which this portal wants to achieve and this request has been encapsulated in this interface. In short- Functionality to achieve is exposed through this interface.
Adaptee: The third party vendor’s code which gives us the list of products.
Adapter: The wrapper which would implement ITarget and would call third party vendor’s code.
Client: The online shopping portal code which gets the list of products and then displays them.
Let’s have a look at the code demonstrating adapter pattern depicting the above scenario then we would revisit the various classes to map them to the adapter pattern participant’s.
interface ITarget
{
  List<string> GetProducts();
}


public class VendorAdaptee
{
   public List<string> GetListOfProducts()
   {
      List<string> products = new List<string>();
      products.Add("Gaming Consoles");
      products.Add("Television");
      products.Add("Books");
      products.Add("Musical Instruments");
      return products;
   }
}


class VendorAdapter:ITarget
{
   public List<string> GetProducts()
   {
      VendorAdaptee adaptee = new VendorAdaptee();
      return adaptee.GetListOfProducts();
   }
}


class ShoppingPortalClient
{
   static void Main(string[] args)
   {
      ITarget adapter = new  VendorAdapter();
      foreach (string product in adapter.GetProducts())
      {
        Console.WriteLine(product);
      }
      Console.ReadLine();
   }
}

In the above code the participants are mapped as: ITarget: interface ITarget Adapter: class VendorAdapter, implementing the ITarget interface and acting as a wrapper/link between VendorAdaptee and ShoppingPortalClient. Adaptee: class VendorAdaptee, this is the code which ShoppingPortalClient is interested in calling. Client: class ShoppingPortalClient, the client wants to call the code of VendorAdptee. The code above is self-explanatory, the client which has access to the interface ITarget wants to call the method VendorAdaptee.GetListOfProduct() but since VendorAdaptee does not have an ITarget interface there was a need to create an adapter VendorAdapter. The VendorAdapter implements ITarget interface and calls the method of adaptee.

Based on how the adapter calls the adaptee it can be named as Class Adapter i.e. when the adapter uses class inheritance to call the Adaptee code. It is called Object Adapter when it uses the object composition to call the adaptee code. For example the VendorAdapter shown above is an object adapter because it creates an instance of (object composition) VendorAdaptee.

Points of Interest

The Adapter design pattern is easy to implement and ensures calling the existing code which was otherwise difficult because their interfaces being incompatible. It is quiet common when legacy code has to be called.

History

Version 1 (16/5/2014)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rahul Dhammy
Architect
India India
I am a software developer by profession presently working as an Associate Technology Architect in India. My passion is technology. I like to learn new things every day and improve my skills in terms of technology. I feel software development can be made very interesting if we inculcate the habit of "beautiful thinking".

Comments and Discussions

 
GeneralMy vote of 3 PinprofessionalCatchExAs18-May-14 7:32 
GeneralMy vote of 5 PinpremiumVolynsky Alex18-May-14 4:58 
GeneralRe: My vote of 5 PinprofessionalRahul Dhammy18-May-14 7:58 
GeneralRe: My vote of 5 PinpremiumVolynsky Alex18-May-14 8:13 
QuestionWhat will happen for different product class for adaptee and client? PinmemberShirajul Mamun18-May-14 1:52 
AnswerRe: What will happen for different product class for adaptee and client? PinprofessionalRahul Dhammy18-May-14 8:04 
GeneralRe: What will happen for different product class for adaptee and client? PinmemberShirajul Mamun18-May-14 19:43 
QuestionMy vote of 5 PinmemberSunam3917-May-14 13:13 
GeneralMy vote of 5 PinmemberKannan.Ramjalwar17-May-14 0:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 17 May 2014
Article Copyright 2014 by Rahul Dhammy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid