Design Pattern

Adapter Design Pattern – Structural Patterns

<p><strong>Adapter Design Pattern<&sol;strong> comes under the <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;">Structural Design Pattern<&sol;a><&sol;strong>&comma; according this design pattern two incompatible classes working together that couldn&&num;8217&semi;t otherwise because of incompatible interfaces&period; This pattern works as a bridge between two incompatible interfaces&period;<&sol;p>&NewLine;<h2>Adapter Design Pattern<&sol;h2>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>According to the Gang of Four&colon;<&sol;strong><&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">Convert the interface of a class into another interface clients expect&period; Adapter lets classes work together that couldn&&num;8217&semi;t otherwise because of incompatible interfaces&period;<&sol;div>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<p>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&period; There are many <em><strong>real life example<&sol;strong><&sol;em> where this pattern involves such as different type of electric plugs like <em><strong>cylindrical<&sol;strong> <&sol;em>and <em><strong>rectangular<&sol;strong> <&sol;em>plugs as the below figure&period; So you can use an adapter in between to fit an rectangular plug in cylindrical socket assuming voltage requirements are met with&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-3683" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;Adapter&period;jpg" alt&equals;"Adapter Pattern" width&equals;"297" height&equals;"239" &sol;><&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Patterns<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2>UML class diagram for the Adapter Pattern<&sol;h2>&NewLine;<p>Let&&num;8217&semi;s see the following class diagram and it illustrates about the component classes and interfaces&period;<&sol;p>&NewLine;<p><img class&equals;"aligncenter size-full wp-image-3684" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;adapter-design-pattern&period;jpg" alt&equals;"adapter design pattern" width&equals;"432" height&equals;"253" &sol;><&sol;p>&NewLine;<p>There are the following specifications for the adapter pattern&colon;<br &sol;>&NewLine;<strong>Target<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>This is the desired interface class which will be used by the clients&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>Adapter<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>This class is a wrapper class which implements the desired target interface and modifies the specific request available from the Adaptee class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>Adaptee<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>This is the class which is used by the Adapter class to reuse the existing functionality and modify them for desired use&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong>Client<&sol;strong><&sol;p>&NewLine;<ul>&NewLine;<li>This class will interact with the Adapter class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bridge-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bridge-design-patterns&sol;">Bridge Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;composite-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;composite-pattern-design-patterns-java&sol;">Composite Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;decorator-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;decorator-design-pattern&sol;">Decorator Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;proxy-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;proxy-pattern-design-patterns-java&sol;">Proxy Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;builder-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;builder-design-pattern&sol;">Builder Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;&sol;">Creational GoF Design Patterns<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;">Behavioral GoF Design Patterns<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<h2>Applicability<&sol;h2>&NewLine;<p>There are following common requirement for the Adapter Pattern where we can use it&period;<&sol;p>&NewLine;<ul>&NewLine;<li>Usage of this pattern in your application&comma; there is a need to use an existing class with an incompatible interface&period;<&sol;li>&NewLine;<li>Another usage of this pattern in your application&comma; when you want to create a reusable class that collaborates with classes which has incompatible interfaces<&sol;li>&NewLine;<li>There are several existing subclasses to be use&comma; but it’s impractical to adapt their interface by sub-classing every one&period;<&sol;li>&NewLine;<li>An object adapter can adapt the interface of its parent class&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>Benefits of the Adapter Design Pattern<&sol;h2>&NewLine;<p>There are following pros of the Adapter Design Pattern&period;<&sol;p>&NewLine;<ul>&NewLine;<li>This pattern allows you to communicate and interact two or more incompatible objects together&period;<&sol;li>&NewLine;<li>Adapter pattern promotes the re-usability of older existing functionality in your application&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>Examples of the Adapter Pattern<&sol;h2>&NewLine;<p>There are following some listed classes based on the Adapter Design Pattern in the Spring Framework<&sol;p>&NewLine;<ul>&NewLine;<li>JpaVendorAdapter<&sol;li>&NewLine;<li>HibernateJpaVendorAdapter<&sol;li>&NewLine;<li>HandlerInterceptorAdapter<&sol;li>&NewLine;<li>MessageListenerAdapter<&sol;li>&NewLine;<li>SpringContextResourceAdapter<&sol;li>&NewLine;<li>ClassPreProcessorAgentAdapter<&sol;li>&NewLine;<li>RequestMappingHandlerAdapter<&sol;li>&NewLine;<li>AnnotationMethodHandlerAdapter<&sol;li>&NewLine;<li>WebMvcConfigurerAdapter<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>Sample Implementation of Adapter Design Pattern<&sol;h2>&NewLine;<p>I am going to create an example which showing the actual demonstration of adapter design pattern&comma; let&&num;8217&semi;s discuss this example&comma; I am creating this example related to two type of the electric sockets for the power supply&period; One is cylindrical and another is rectangular&period; But in the India&comma; we are using cylindrical socket only&comma; if you want to use rectangular socket then you have to use electric socket adapter&period; Let&&num;8217&semi;s see the code&period;<&sol;p>&NewLine;<h3>Step 1&colon; Create Cylindrical Socket class&period;<&sol;h3>&NewLine;<p><strong>CylindricalSocket&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;adapter&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class CylindricalSocket &lbrace; &NewLine; &NewLine;&Tab;public void supply&lpar;String cylinStem1&comma; String cylinStem2&rpar; &lbrace; &NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Power power power&period;&period;&period;"&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 2&colon; Create Rectangular Socket class&period;<&sol;h3>&NewLine;<p><strong>RectangularSocket&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;adapter&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class RectangularSocket &lbrace; &NewLine;&Tab; &NewLine;&Tab;private String rectaStem1&semi; &NewLine;&Tab;private String rectaStem2&semi; &NewLine;&Tab; &NewLine;&Tab;public void getPower&lpar;&rpar; &lbrace; &NewLine;&Tab;&Tab;RectangulrAdapter adapter &equals; new RectangulrAdapter&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;adapter&period;adapt&lpar;rectaStem1&comma; rectaStem2&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 3&colon; Let&&num;8217&semi;s create an Adapter class&period;<&sol;h3>&NewLine;<p><strong>RectangulerAdapter&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;adapter&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class RectangulerAdapter &lbrace; &NewLine;&Tab; &NewLine;&Tab;private CylindricalSocket socket &equals; new CylindricalSocket&lpar;&rpar;&semi; &NewLine;&Tab; &NewLine;&Tab;public void adapt&lpar;String rectaStem1&comma; String rectaStem2&rpar; &lbrace; &NewLine;&Tab;&Tab;&sol;&sol;some conversion logic &NewLine;&Tab; String cylinStem1 &equals; rectaStem1&semi; &NewLine;&Tab; String cylinStem2 &equals; rectaStem2&semi; &NewLine;&Tab; socket&period;supply&lpar;cylinStem1&comma; cylinStem2&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 4&colon; Let&&num;8217&semi;s create a demo class&period;<&sol;h3>&NewLine;<p><strong>AdapterPatternDemo&period;java<&sol;strong><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;patterns&period;structural&period;adapter&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class AdapterPatternDemo &lbrace; &NewLine; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab;RectangularSocket socket &equals; new RectangularSocket&lpar;&rpar;&semi; &NewLine;&Tab;&Tab;socket&period;getPower&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3>Step 5&colon; Let&&num;8217&semi;s run this demo class and verify output&period;<&sol;h3>&NewLine;<pre class&equals;"highlight"> &NewLine;Power power power&period;&period;&period; &NewLine; &NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;singleton-design-pattern&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;bridge-design-patterns&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '3682'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

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…

4 years ago

Decorator Pattern using Lambda

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

4 years ago

Delegating pattern using lambda

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

4 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;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

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

4 years ago

Scheduler @Scheduled Annotation Spring Boot

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

4 years ago