What is Adapter Design Pattern?
Adapter Design Pattern behaves like a bridge between two interfaces that are incompatible. This Adapter Design Pattern incorporates one adapter class that is responsible for joining functionalities of interfaces that are incompatible or independent.
Real Time Example for Adapter Design Pattern
Best real time example for Adapter Design Pattern will be the card reader. The card reader is playing the role of an adapter between laptop and memory card. The memory card is plugged into the card reader and then the card reader is plugged into the laptop so that the memory card is read through laptop.
Purpose of Adapter Design Pattern
- Based on Clients expectation, convert one class interface into another interface
- Permit classes to work together irrespective of their incompatible interfaces
Adapter Design Pattern Implementation using Java
To demonstrate the Adapter Design Pattern, let us consider two shapes namely Square and Rectangle as example. Square and Rectangle are two different shapes, but when the length and width of a rectangle are equal, then it forms a square. Using this geometric condition, we are going to create an adapter for Rectangle to accommodate a square.
We will create an interface called “iSquare” which contains a method “calcArea” to calculate area of the square. This method will take only one parameter representing the side of the square.
1 2 3 |
public interface iSquare { public abstract void calcArea(int squareSide); } |
Then we will create a class that implements the interface and that class method will use appropriate formula to calculate area. This is demonstrated in the code snippet below:
1 2 3 4 5 6 7 |
public class Square implements iSquare { @Override public void calcArea(int squareSide) { int area = Math.pow(squareSide, 2); System.out.println("Area is "+area); } } |
Now, we will create another interface called “iRectangle” which contains a method “calcArea” to calculate area of the Rectangle. This method will take two parameters representing the length and width of the Rectangle.
1 2 3 |
public interface iRectangle { public abstract void calcArea(int rectLength, int rectWidth); } |
Then we will create a class that implements this interface and its associated abstract method.
1 2 3 4 5 6 7 8 |
public class Rectangle implements iRectangle { @Override public void calcArea(int rectLength, int rectWidth) { int area = rectLength * rectWidth; System.out.println("Area is " +area); } } |
Till now, it was a simple straight-forward example. Now our intention is to calculate area of the Square using an incompatible interface iRectangle. In order to achieve this, lets create an adapter class called RectSquareAdapter.
1 2 3 4 5 6 7 8 9 10 11 |
public class RectSquareAdapter implements iSquare { private iRectangle rect = null; public RectSquareAdapter(iRectangle rect){ this.rect = rect; } @Override public void calcArea(int rectLength){ rect.calcArea(rectLength, rectLength); } } |
This adapter class implements iSquare interface and within its calcArea method, it attempted to call calcArea method of iRectangle interface. Lets now test this adapter class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class TestAdapter{ public static void main(String[] args){ iSquare squareObject = new Square(); System.out.println("Calculating area of square with side as 10 using Square class..."); squareObject.calcArea(10); iRectangle rectObject = new Rectangle(); System.out.println("Calculating area of rectangle with length as 5 and width as 4 using Rectangle class..."); rectObject.calcArea(5, 4); rectObject = new Rectangle(); System.out.println("Calculating area of square with side as 5 using rectangle with the help of Adapter class... "); RectSquareAdapter adapterObject = new RectSquareAdapter(rectObject); adapterObject.calcArea(5); } } |
Output will be:
Calculating area of square with side as 10 using Square class…
Area is 100.0
Calculating area of rectangle with length as 5 and width as 4 using Rectangle class…
Area is 20
Calculating area of square with side as 5 using rectangle with the help of Adapter class…
Area is 25
Thus these programs successfully demonstrate the adapter pattern by calculating area of square using the method of rectangle.
Leave a Reply