Design Pattern

Adapter Design Pattern – Structural Patterns

Adapter Design Pattern comes under the Structural Design Pattern, according this design pattern two incompatible classes working together that couldn’t otherwise because of incompatible interfaces. This pattern works as a bridge between two incompatible interfaces.

Adapter Design Pattern

According to the Gang of Four:

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

This pattern is used when the interfaces are incompatible and functionality need to be integrated these incompatible interfaces to each other with making any change in the existing source code. There are many real life example where this pattern involves such as different type of electric plugs like cylindrical and rectangular plugs as the below figure. So you can use an adapter in between to fit an rectangular plug in cylindrical socket assuming voltage requirements are met with.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Patterns“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.

UML class diagram for the Adapter Pattern

Let’s see the following class diagram and it illustrates about the component classes and interfaces.

There are the following specifications for the adapter pattern:
Target

  • This is the desired interface class which will be used by the clients.

Adapter

  • This class is a wrapper class which implements the desired target interface and modifies the specific request available from the Adaptee class.

Adaptee

  • This is the class which is used by the Adapter class to reuse the existing functionality and modify them for desired use.

Client

  • This class will interact with the Adapter class.

Applicability

There are following common requirement for the Adapter Pattern where we can use it.

  • Usage of this pattern in your application, there is a need to use an existing class with an incompatible interface.
  • Another usage of this pattern in your application, when you want to create a reusable class that collaborates with classes which has incompatible interfaces
  • There are several existing subclasses to be use, but it’s impractical to adapt their interface by sub-classing every one.
  • An object adapter can adapt the interface of its parent class.

Benefits of the Adapter Design Pattern

There are following pros of the Adapter Design Pattern.

  • This pattern allows you to communicate and interact two or more incompatible objects together.
  • Adapter pattern promotes the re-usability of older existing functionality in your application.

Examples of the Adapter Pattern

There are following some listed classes based on the Adapter Design Pattern in the Spring Framework

  • JpaVendorAdapter
  • HibernateJpaVendorAdapter
  • HandlerInterceptorAdapter
  • MessageListenerAdapter
  • SpringContextResourceAdapter
  • ClassPreProcessorAgentAdapter
  • RequestMappingHandlerAdapter
  • AnnotationMethodHandlerAdapter
  • WebMvcConfigurerAdapter

Sample Implementation of Adapter Design Pattern

I am going to create an example which showing the actual demonstration of adapter design pattern, let’s discuss this example, I am creating this example related to two type of the electric sockets for the power supply. One is cylindrical and another is rectangular. But in the India, we are using cylindrical socket only, if you want to use rectangular socket then you have to use electric socket adapter. Let’s see the code.

Step 1: Create Cylindrical Socket class.

CylindricalSocket.java

/**
 * 
 */
package com.doj.patterns.structural.adapter;

/**
 * @author Dinesh.Rajput
 *
 */
public class CylindricalSocket {

	public void supply(String cylinStem1, String cylinStem2) {
		System.out.println("Power power power...");
	}
}

Step 2: Create Rectangular Socket class.

RectangularSocket.java

/**
 * 
 */
package com.doj.patterns.structural.adapter;

/**
 * @author Dinesh.Rajput
 *
 */
public class RectangularSocket {
	
	private String rectaStem1;
	private String rectaStem2;
	
	public void getPower() {
		RectangulrAdapter adapter = new RectangulrAdapter();
		adapter.adapt(rectaStem1, rectaStem2);
	}
}

Step 3: Let’s create an Adapter class.

RectangulerAdapter.java

/**
 * 
 */
package com.doj.patterns.structural.adapter;

/**
 * @author Dinesh.Rajput
 *
 */
public class RectangulerAdapter {
	
	private CylindricalSocket socket = new CylindricalSocket();
	
	public void adapt(String rectaStem1, String rectaStem2) {
		//some conversion logic
	    String cylinStem1 = rectaStem1;
	    String cylinStem2 = rectaStem2;
	    socket.supply(cylinStem1, cylinStem2);
	}
}

Step 4: Let’s create a demo class.

AdapterPatternDemo.java

/**
 * 
 */
package com.doj.patterns.structural.adapter;

/**
 * @author Dinesh.Rajput
 *
 */
public class AdapterPatternDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		RectangularSocket socket = new RectangularSocket();
		socket.getPower();
	}

}

Step 5: Let’s run this demo class and verify output.

Power power power...

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago