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.
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.
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
Adapter
Adaptee
Client
also read:
There are following common requirement for the Adapter Pattern where we can use it.
There are following pros of the Adapter Design Pattern.
There are following some listed classes based on the Adapter Design Pattern in the Spring Framework
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.
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..."); } }
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); } }
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); } }
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(); } }
Power power power...
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…