Design Pattern For Beginners - Part 6: 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.

using System;
using System.Collections; 
namespace AdpatorDesign
{
    interface
ILaptop
    {
        void ShowModel();
    }
    class HP_Laptop:
ILaptop
    {
        public void ShowModel()
        {
            Console.WriteLine("I am HP Laptiop");
        }
    }
    class Sony_Laptop :
ILaptop
    {
        public void ShowModel()
        {
            Console.WriteLine("I am Sony Laptop");
        }
    }
    class Compaq_Laptop :
ILaptop
    {
        public void ShowModel()
        {
            Console.WriteLine("I am Compaq Laptop");
        }
    } 
    class LaptopAdaptor :
ILaptop
    {
        public void ShowModel(){}
        public static void ShowModel(ILaptop obj)
        {
            obj.ShowModel();
        }
    } 
    class
Person
    {
        public void SwitchOn(ILaptop obj)
        {
            LaptopAdaptor.ShowModel(obj);  
        }
    } 
    class
Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.SwitchOn(new HP_Laptop());
//On HP Laptop
            p.SwitchOn(new Compaq_Laptop()); //On Compaq laptop 
            Console.ReadLine();
        }
    }
}

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

Up Next
    Ebook Download
    View all
    Learn
    View all