Adapter design pattern for beginners.

Background –


Think about a situation where a Chinese person has to communicate to an Indian person. Where Chinese person doesn’t speak Hindi, and Indian person doesn’t speak Chinese. I know you already knew the solution :). Find a third person who understand Chinese, and Hindi both, and translate both ways so that they can communicate.

Congratulations! If you understand above mentioned small concept, you know, adapter design pattern as well.

This design pattern had come from the hardware industry. Look at the below mentioned diagram -



You must have seen many hardware adapters in your life. India's electronic boards designed for round pins, but not in the US. So if you have purchased your laptop from the US, you will have a different charge pin. Like mentioned in the above diagram. You need an adapter.

When homogeneous systems talks to each other. We need adapters to make the compatible connections/communication. In software, we have different OS, language, protocols, standards, etc. how all various components will talk to each other? The answer is through adaptor.

The final example is Java language. How it is a platform-independent language? Again because of adaptor design pattern. JVM will take care of different platforms (8-bit, 16-bit, 32-bit, 64-bit …etc). JVM act as adaptor here.

About adapter design pattern –


“Works between two incompatible interfaces.”


Structure –


Note - We have two kinds of adapter implementations. First class-based, which you can see in the diagram mentioned above. Second is object-based. If your programming language doesn't support multiple inheritance, and if you don't wish to solve your problem using multiple inheritance in C++. In both, cases you can solve it through object-based adapter.

Example –





#include <iostream>
using namespace std;
enum Platform
{
    _8BIT,
    _16BIT,
    _32BIT,
    _64BIT
};

class RunMeAnywhere
{
    public:
        void run()
        {
            cout<< "Running the actual code" << endl;
        }
};

class IRunAnywhere
{
    public:
        virtual void execute() = 0;
};

class Adapter : public IRunAnywhere, private RunMeAnywhere
{
    public:
        virtual void execute()
        {
            cout<< "Running through adapter"<<endl;
            run();
        }
};

class _8BitStruture : public Adapter
{
    public:
        void execute()
        {
            cout<< "Running in 8 bit hardware architecture "<<endl;
            Adapter::execute();
        }
};
class _16BitStruture : public Adapter
{
    public:
        void execute()
        {
            cout<< "Running in 16 bit hardware architecture "<<endl;
            Adapter::execute();
        }
};
class _32BitStruture : public Adapter
{
    public:
        void execute()
        {
            cout<< "Running in 32 bit hardware architecture "<<endl;
            Adapter::execute();
        }
};
class _64BitStruture : public Adapter
{
    public:
        void execute()
        {
            cout<< "Running in 64 bit hardware architecture "<<endl;
            Adapter::execute();
        }
};
/*
#######################################################################################
#########  __  __       _        ######################################################
######### |  \/  | __ _(_)_ __   ######################################################
######### | |\/| |/ _` | | '_ \  ######################################################
######### | |  | | (_| | | | | | ######################################################
######### |_|  |_|\__,_|_|_| |_| ######################################################
#######################################################################################
*/
int main(int argc, char** argv)
{
    IRunAnywhere *_8bitRun = new _8BitStruture;
    IRunAnywhere *_16bitRun = new _16BitStruture;
    IRunAnywhere *_32bitRun = new _32BitStruture;
    IRunAnywhere *_64bitRun = new _64BitStruture;
    
    _8bitRun->execute();
    _16bitRun->execute();
    _32bitRun->execute();
    _64bitRun->execute();
    return 0;
}

///////////////////////////////////////////////////////////////////-------------OutPut
/*
$ ./a.exe
Running in 8 bit hardware architecture
Running through adapter
Running the actual code
Running in 16 bit hardware architecture
Running through adapter
Running the actual code
Running in 32 bit hardware architecture
Running through adapter
Running the actual code
Running in 64 bit hardware architecture
Running through adapter
Running the actual code

*/

Example 2 - 

know





 #include<iostream>
 using namespace std;
 class Calculator
 {
  public:
  virtual int plus(int firstInput, int secondValue) = 0;
  virtual int subtraction(int firstInput, int secondValue) = 0;
  virtual int multiplication(int firstInput, int secondValue) = 0;
  virtual int division(int firstInput, int secondValue) = 0;
 };
 //*************************************************************************************
 // Class HindiCalculations, data members and member methods are in Hindi,
 // calculation is done by Hindi interface but we can execute it through EnglishCalculatorAdapter
 class HindiCalculations
 {
  public:
  int dhana(int pahaliSankyaa, int dusariSankyaa)
  {
   return (pahaliSankyaa + dusariSankyaa);
  }
  int ghataanaa(int pahaliSankyaa, int dusariSankyaa)
  {
   return (pahaliSankyaa - dusariSankyaa);
  }
  int gunaa(int pahaliSankyaa, int dusariSankyaa)
  {
   return (pahaliSankyaa * dusariSankyaa);
  }
  int bhaaga(int pahaliSankyaa, int dusariSankyaa)
  {
   return (pahaliSankyaa / dusariSankyaa);
  }
};
 //*************************************************************************************
 // Class calculator, this is just an example class to show Adaptor, so I have implemented  
 // ot only for int. It is not a full Calculator class.
 class EnglishCalculatorAdapter: public Calculator, private HindiCalculations
 {
  public:
  int plus(int firstInput, int secondValue)
  {
   cout<< "Doing mathematical plus operation through EnglishCalculatorAdapter"<<endl;
   cout<< "Actual implementaion present in HindiCalculations class"<<endl;
   return dhana(firstInput,secondValue);
  }
  int subtraction(int firstInput, int secondValue)
  {
   cout<< "Doing mathematical subtraction operation through EnglishCalculatorAdapter"<<endl;
   cout<< "Actual implementaion present in HindiCalculations class"<<endl;
   return ghataanaa(firstInput,secondValue);
  }
  int multiplication(int firstInput, int secondValue)
  {
   cout<< "Doing mathematical multiplication operation through EnglishCalculatorAdapter"<<endl;
   cout<< "Actual implementaion present in HindiCalculations class"<<endl;
   return gunaa(firstInput,secondValue);
  }
  int division(int firstInput, int secondValue)
  {
   cout<< "Doing mathematical division operation through EnglishCalculatorAdapter"<<endl;
   cout<< "Actual implementaion present in HindiCalculations class"<<endl;
   return bhaaga(firstInput,secondValue);
  }
};


 int main(int argc, char** argv)
 {
  int tempValueOne = 20;
  int tempValueTwo = 10;
  
  Calculator* EnglishCalculator = new EnglishCalculatorAdapter;
  cout<< "result - " << EnglishCalculator->plus(tempValueOne,tempValueTwo) << endl;
  cout<< "result - " << EnglishCalculator->subtraction(tempValueOne,tempValueTwo)<< endl;
  cout<< "result - " << EnglishCalculator->multiplication(tempValueOne,tempValueTwo)<< endl;
  cout<< "result - " << EnglishCalculator->division(tempValueOne,tempValueTwo)<< endl;
  return 0;
 }

 
 ///////////////////////////////////////////////////////////////////-------------OutPut
 /*
  $ ./a.exe
  Doing mathematical plus operation through EnglishCalculatorAdapter
  Actual implementaion present in HindiCalculations class
  result - 30
  Doing mathematical subtraction operation through EnglishCalculatorAdapter
  Actual implementaion present in HindiCalculations class
  result - 10
  Doing mathematical multiplication operation through EnglishCalculatorAdapter
  Actual implementaion present in HindiCalculations class
  result - 200
  Doing mathematical division operation through EnglishCalculatorAdapter
  Actual implementaion present in HindiCalculations class
  result - 2

 */



Thanks for reading it. To learn more about design patterns and basic design principles, please see my web page.

Comments

Popular posts from this blog

Non-virtual interface idiom (NVI)

Architectural patterns => Mud to structure => layers.

Architectural style -> Adoptable system -> Reflection.