Adapter Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

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:

Generic Adapter Design Pattern

Generic Adapter Design Pattern

  • 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:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout adapter

If you don’t have git installed, you can either directly download zip or copy paste from below.

Square.java

Rectangle.java

SquareImpl.java

RectangleImpl.java

RectangleAdapter.java

  • 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

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:

Adapter Pattern Example

Adapter Pattern Example

I hope this article helped understand adapter design pattern implementation.

 

Share Button

1 comment for “Adapter Design Pattern Implementation

  1. Nyng
    May 12, 2017 at 1:10 pm

    The name “Rectangle Adapter” is a little bit confusing,
    but I know you mean “an adapter containing/using a rectangle”.

Leave a Reply

Your email address will not be published. Required fields are marked *