• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Adapter Design Pattern Implementation using Java

July 18, 2017 By j2eereference Leave a Comment

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.

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

Java
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.

Java
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.

Java
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.

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

 

Java
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.  

Related Posts

  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • GOF Design Patterns
  • History and Need for Design Patterns
  • Command Design Pattern
  • Template Design Pattern
  • Proxy design pattern
  • Singleton in Clustered Environment
  • Observer pattern

Filed Under: Design Patterns

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.