Adapter Design Pattern Implementation
Latest posts by Prasad Kharkar (see all)
- PyCharm for Machine Learning - July 17, 2018
- Linear Discriminant Analysis using Python - April 30, 2018
- Principal Component Analysis using Python - April 30, 2018
From this article onwards we are going to see structural design patterns. Adapter design pattern implementation is one of the most widely used pattern in real life scenario as well as software applications.
Adapter Design Pattern Implementation:
Gang of Four book defines it as,
The Adapter pattern converts interface of a class into another interface the client expects. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
Generic Class Diagram:

- Target: The class of which functionality is being used by client.
- Adaptee: The class of which functionality we can’t use because of incompatible interface
- Adapter: The class which is going to act as converted from Target class to Adaptee class to make them compatible with each other with Adapter as mediator
- Client: Actual class from where all others are being used.
Adapter Design Pattern Implementation Example:
Consider situation as below.
- We have an interface Square, having method calculateArea(int side) which takes one parameter.
- We have another interface Rectangle having method calculateArea(int length, int width) which takes two parameters.
- Now a Square can also be considered as a Rectangle due to its geometrical properties. Square should also be able to calculate area using length and width.
- In current situation, calculateArea(int) from Square can only take a side and not both length and width
- To solve this, we can create an intermediate adapter class which can use functionality of Rectangle.
Let us directly go ahead with code.
If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.
Go to your working directory and then follow below steps:
- git clone https://github.com/theJavaGeek/design-patterns.
- cd design-patterns
- git checkout adapter
If you don’t have git installed, you can either directly download zip or copy paste from below.
Square.java
1 2 3 4 5 |
package com.thejavageek.designpatterns; public interface Square { public abstract int calculateArea(int side); } |
Rectangle.java
1 2 3 4 5 |
package com.thejavageek.designpatterns; public interface Rectangle { public abstract int calculateArea(int length, int width); } |
SquareImpl.java
1 2 3 4 5 6 7 8 9 10 |
package com.thejavageek.designpatterns; public class SquareImpl implements Square { @Override public int calculateArea(int side) { return (int)Math.pow(side, 2); } } |
RectangleImpl.java
1 2 3 4 5 6 7 8 9 10 |
package com.thejavageek.designpatterns; public class RectangleImpl implements Rectangle { @Override public int calculateArea(int length, int width) { return length * width; } } |
RectangleAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.thejavageek.designpatterns; public class RectangleAdapter implements Square { private Rectangle rectangle = null; public RectangleAdapter(Rectangle rectangle){ this.rectangle = rectangle; } @Override public int calculateArea(int length) { return rectangle.calculateArea(length, length); } } |
- We implemented Square interface as we need to use calculateArea() from Square and call Rectangle functionality there.
- To do that, we defined an instance variable of type Rectangle.
- Notice that we have called rectangle.calculateArea(side, side);
Let us test our adapter design pattern implementation
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.thejavageek.designpatterns; public class AdapterTest { public static void main(String[] args) { Square square = new SquareImpl(); Rectangle rectangle = new RectangleImpl(); RectangleAdapter adapter = new RectangleAdapter(rectangle); System.out.println(adapter.calculateArea(10)); } } |
This prints 100 i.e. it successfully calculates area of a square using a rectangle method.
- First we create a Square and then a Rectangle.
- Then we created a RectangleAdapter and pass newly created Rectangle object to it as we have defined such constructor in it.
- Then we call adapter.calculateArea(10); which is possible because RectangleAdapter implements Square which in turn calls calculateArea(10,10); on parameter passed to the constructor.
Example Class Diagram:

I hope this article helped understand adapter design pattern implementation.
The name “Rectangle Adapter” is a little bit confusing,
but I know you mean “an adapter containing/using a rectangle”.