Adapter pattern is useful when one client class wants to use another client class with an incompatible interface.

Adapter pattern works as a bridge between two incompatible interfaces.

Here is an example of using Adapter pattern:

Consider, we have a Rectangle class with properties height and width and a getArea() method that returns its Area.

It implements a RectangularShape interface with getArea() method. The client uses its reference and calls getArea() to find rectangle’s area.

Now we have a new Triangle shape. The client wants to find area of the triangle using same interface as it did before.

But since triangle’s interface is different from rectange’s, so we need an adapter in between.

### RectangularShape interface

package com.topjavatutorial.patterns.adapter; public interface RectangularShape { public double getArea(); }

### Rectangle class

package com.topjavatutorial.patterns.adapter; public class Rectangle implements RectangularShape{ double length; double breadth; public Rectangle(double length, double breadth) { this.length = length; this.breadth = breadth; } @Override public double getArea() { // TODO Auto-generated method stub return length * breadth; } }

But the Client wants to use it to calculate area of a Triangle.

Here is the triangle class.

### Triangle class

package com.topjavatutorial.patterns.adapter; public class Triangle{ double base; double height; public Triangle(double base, double height) { this.base = base; this.height = height; } }

Lets create an Adapter that will use the getArea() method of Rectangle to calculate area of Triangle.

For this, we need the Adapter to implement the interface of the type its adapting to. Since, its adapting to a Rectangle, lets implement its supertype.

We also need a Triangle object reference since we will be calculating its area.

### Adapter class

package com.topjavatutorial.patterns.adapter; public class Adapter implements RectangularShape { Triangle t; Rectangle r; public Adapter(Triangle t) { this.t = t; } @Override public double getArea() { r = new Rectangle(0.5 * t.base,t.height); double area = r.getArea(); return area; } }

Inside the getArea() method of this Adapter, we are using logic to calculate triangle’s area.

Note that, the adapter implements RectangularShape which is the supertype of Rectangle.

### Client to test Adaptor implementation

package com.topjavatutorial.patterns.adapter; public class AdapterPatternDemo { public static void main(String[] args) { //Calculating area of Rectangle shape RectangularShape r1 = new Rectangle(10, 5); double rectangleArea = r1.getArea(); System.out.println("Rectangle area = " + rectangleArea); //Calculating area of Triangle shape using an Adapter Triangle t = new Triangle(10, 5); RectangularShape r2 = new Adapter(t); double triangleArea = r2.getArea(); System.out.println("Triangle area = " + triangleArea); } }

#### Output

Rectangle area = 50.0

Triangle area = 25.0

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

#