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.

Adapter Pattern

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“.
Spring-5-Design-Pattern

UML class diagram for the Adapter Pattern

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

adapter design pattern

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