Sunday, March 29, 2020

Adapter Design Pattern


As we discussed earlier there are lots of design pattern in OOPs. We discussed following design pattern:

Singleton design pattern
Abstract factory design pattern
Decorator design pattern
Flyweight design pattern
Observer design pattern

In this blog, we discuss Adapter design pattern is one of the structural design patterns which is used for to interface two unrelated work with each other.

We are all familiar with adapter, it is a device which is used to connect two incompatible devices to do some work. Adapter which can put into the power socket and then put your plug into another end of the adapter. The adapter changes the form of your plug so that can use it with the power socket. But adapter doesn’t provide any additional functionality. It just connects plug and power supply.
 
For example, in real time application, to read the memory card via laptop it is impossible without adapter. So, in this situation card reader is act as a adapter between laptop and memory card. If we plugin the memory card into the card reader and card reader into a laptop. Then memory card can be easily read via laptop.

In this design pattern, the work of adapter design pattern same like adapter device. Here adapter convert interface of one class into interface of another class that the client expects. Adapter design pattern is also known as the wrapper. 

There are two types of adapters:
  • Object Adapter 
  • Class Adapter

Object Adapter:
Object adapters use a compositional technique to adapt one interface to another inaterface. The adapter inherits the target interface that the client want to see. It hold an instance the adaptee (Target and adaptee is also called as legacy system which is pre-exist). When the client call the request method on its target object, the request is translated into the corresponding specific request on the adaptee. Object adapter provide that the client completely decoupled from the adaptee. Only adapter knows about both of them. Below block diagram shows the object adapter design pattern. 

Class Adapter:
Class adapter uses multiple inheritance and can only wrap a class. As in the object adapter, the class adapter inherits the interface of the adaptee as well. But java does not support multiple inheritance, this means that one of the interfaces must be inherited from a java interface type. The class adapter subclass the target and the adaptee. If client request to the target is simply rerouted to the specific request which is inherit from the adaptee interface. Fig below shows the block diagram of class adapter design pattern.


Code: 
#include <iostream>
using namespace std;
typedef int Coordinate;
typedef int Dimension;

// Desired interface (Target)
class Rectangle
{
  public:
    virtual void draw() = 0;
};

// Legacy component (Adaptee)
class LegacyRectangle
{
  public:
    LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2, Coordinate y2)
    {
        x1_ = x1;
        y1_ = y1;
        x2_ = x2;
        y2_ = y2;
        cout << "LegacyRectangle:  create.  (" << x1_ << "," << y1_ << ") => ("
          << x2_ << "," << y2_ << ")" << endl;
    }
    void oldDraw()
    {
        cout << "LegacyRectangle:  oldDraw.  (" << x1_ << "," << y1_ <<
          ") => (" << x2_ << "," << y2_ << ")" << endl;
    }
  private:
    Coordinate x1_;
    Coordinate y1_;
    Coordinate x2_;
    Coordinate y2_;
};

// Adapter wrapper
class RectangleAdapter: public Rectangle, private LegacyRectangle
{
  public:
    RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h):
      LegacyRectangle(x, y, x + w, y + h)
    {
        cout << "RectangleAdapter: create.  (" << x << "," << y <<
          "), width = " << w << ", height = " << h << endl;
    }
    virtual void draw()
    {
        cout << "RectangleAdapter: draw." << endl;
        oldDraw();
    }
};

int main()
{
  Rectangle *r = new RectangleAdapter(120, 200, 60, 40);   //Request for target
  r->draw();
}

Result:



Explanation:
  •  In main() client class the Rectangle class. So, here Rectangle is a target.
  • As adapter design pattern, inherit multiple classes. Here class RectangleAdapter inherits the Rectangle(base) and LegacyRectangle(Adaptee).
  • The Adapter RectangleAdapter lets the LegacyRectangle responds to request(draw() on a Rectangle)by inheriting both classes.
  • The LegacyRectangle class does not have the same methods(draw())as Rectangle, but the Adapter (RectangleAdapter)can takes the Rectangle method calls and invoke method on the LegacyRectangle, oldDraw(). 




Video Link of Blog: https://drive.google.com/a/vit.edu/file/d/104WSJuztOzL-4ZASIsFXIO8BHP9tK3Py/view?usp=drivesdk









Course Project: 3 Lane Car Game

Course Project Title:  3 lane car racing game Course Project Description:              The aim of our course project is to implement...