Design Pattern For Beginners - Part 6: Adaptor Design Pattern

In today's article we will discuss a very important design pattern called Adaptor Design Pattern.

Welcome to the Design Pattern for Beginners article series. I have been writing about design patterns and implementations in a few articles. If you are also passionate to learn design patterns like me, please feel free to visit my previous articles here:

In today's article we will discuss a very important design pattern called "Adaptor Design Pattern". As the name suggests, it performs operations very similar to adaptors (Hum; laptop adaptor). Before starting the explanation and example code we will try to understand the real scenario where the Adaptor Design Pattern is relevant.

Why adaptor pattern?

A few years ago I bought a mobile phone (at that time it was a little costly and a decent one with very few features) and had been using it for a couple of years. One day I discovered that the charger for the phone was not working, I went to the mobile shop with both charger and phone. The shop checked both and told me "The charger is gone". Alas! Again I need to buy a new one. The next statement of shopkeeper made me more sorrowful, "This type of charger is out of the market. Since the model is very old, the company has stopped manufacturing it". But he continued, the solution is, you need to buy a multi-pin charger, then you can charge your mobile with that.

image1.gif

I know, while you were reading the paragraph above, you were thinking. Hmm, Sourav is telling a story. Yes my dear reader, this is a story and using it we are attempting to understand the basic need for a Adaptor Design Pattern.

Basically the Adaptor Design Pattern is relevant when two different classes talk with each other. Sometimes a situation may occur like that, you cannot change anything in an existing class and at the same time another class wants to talk with the existing class. In that situation we can implement a middle-level class called an adaptor class and by using the adaptor class both classes are able to talk with each other.

How to implement?


When we use the term adaptor, at first laptop adaptors comes to mind. Right? OK, let's implement an adaptor pattern concept with a few laptop classes.

In the following example we have implemented an ILaptop interface in a few laptop classes. All classes have one common function called ShowModel(). If we call the ShowModel() function then it will show the laptop brand.

  1. using System;  
  2. using System.Collections;   
  3. namespace AdpatorDesign  
  4. {  
  5.     interface ILaptop  
  6.     {  
  7.         void ShowModel();  
  8.     }  
  9.     class HP_Laptop:ILaptop  
  10.     {  
  11.         public void ShowModel()  
  12.         {  
  13.             Console.WriteLine("I am HP Laptiop");  
  14.         }  
  15.     }  
  16.     class Sony_Laptop : ILaptop  
  17.     {  
  18.         public void ShowModel()  
  19.         {  
  20.             Console.WriteLine("I am Sony Laptop");  
  21.         }  
  22.     }  
  23.     class Compaq_Laptop : ILaptop  
  24.     {  
  25.         public void ShowModel()  
  26.         {  
  27.             Console.WriteLine("I am Compaq Laptop");  
  28.         }  
  29.     }   
  30.     class LaptopAdaptor :ILaptop  
  31.     {  
  32.         public void ShowModel(){}  
  33.         public static void ShowModel(ILaptop obj)  
  34.         {  
  35.             obj.ShowModel();  
  36.         }  
  37.     }   
  38.     class Person  
  39.     {  
  40.         public void SwitchOn(ILaptop obj)  
  41.         {  
  42.             LaptopAdaptor.ShowModel(obj);    
  43.         }  
  44.     }   
  45.     class Program  
  46.     {  
  47.         static void Main(string[] args)  
  48.         {  
  49.             Person p = new Person();  
  50.             p.SwitchOn(new HP_Laptop()); //On HP Laptop  
  51.             p.SwitchOn(new Compaq_Laptop()); //On Compaq laptop   
  52.             Console.ReadLine();  
  53.         }  
  54.     }  

The Person class works with various types of laptops. And the SwitchOn() function will switch on the proper type of laptop through the LaptopAdaptor class. Here is sample output.

image2.gif