Adapter Design Pattern
Adapter , Adapter , Adapter ~ Actually Adapter design pattern can consider as a simple conversion program / class. It usually used to make two incompatible interfaces or classes to work together. Please review below Adapter design pattern image draw by me, a bit ugly 🙂
Case Study
Company A developed a program in Java to display all of the products’ detail in console. This Java program is very simple and it take an Iterator collection and iterate it to display product one by one.
However Company A outsource Product back end system to a vendor called Vendor B. Vendor B came out a system which will return all products as Enumeration collection.
Arg…wait… return Product as Enumeration? But current Company A system is design to accept product as Iterator. Company A design are all base on Iterator collection to function, now Vendor B provide an old and obsolete function (Enumeration), what should we do?
Above scenarios sound similar to everyone right? Yes it always happened to me as well. So .. who going to change it? To be frankly, i can guarantee Vendor B 100% will not change it for you.
However company A also can not afford to redesign it because it invested so much time and manpower effort already.
Here Adapter come in place. What we need to do is create an adapter class which can convert Enumeration to Iterator. Understand? Study below code may make sense to you.
Code Study
This is an “EnumProduct” class provided by vendor, it will return Product as Enumeration by calling getProduct () function.
package com.mkyong.adapter;
import java.util.Enumeration;
import java.util.Vector;
public class EnumProduct
{
private Vector product;
public EnumProduct(){
product = new Vector();
setProduct("ProductA");
setProduct("ProductB");
setProduct("ProductC");
}
public void setProduct(String s){
product.add(s);
}
public Enumeration getProduct(){
Enumeration eProduct = product.elements();
return eProduct;
}
}
Here is Company A “Product” program which is use to display all products on console screen.
package com.mkyong.adapter;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Iterator;
public class Product
{
//cant change here for some reason
public void displayProduct(Iterator iterator){
for (; iterator.hasNext();)
System.out.println(iterator.next());
}
public static void main(String[] args) {
Product product = new Product();
//product.displayProduct(); //display it
}
}
Company A use displayProduct(Iterator iterator) function to display all of the products information in console. Company A do not want to change and insist want accept Iterator as parameter.
Here Adapter Design Pattern can make it work without any changes involve in current system and vendor’s class as well, what Company A need to do is create an Adapter class to convert Enumeration to Iterator like below
package com.mkyong.adapter;
import java.util.Iterator;
import java.util.Enumeration;
public class EnumToIteratorAdapter implements Iterator
{
Enumeration enumA;
public EnumToIteratorAdapter(Enumeration e){
enumA = e;
}
public boolean hasNext(){
return enumA.hasMoreElements();
}
public Object next(){
return enumA.nextElement();
}
public void remove(){
throw new UnsupportedOperationException();
}
}
Ok, now revisit Company A “Product” class, it can accept Enumeration now!!!
package com.mkyong.adapter;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Iterator;
public class Product
{
public void displayProduct(Iterator iterator){
for (; iterator.hasNext();)
System.out.println(iterator.next());
}
public static void main(String[] args) {
Product product = new Product();
EnumProduct enumProduct = new EnumProduct();
EnumToIteratorAdapter enumToIteratorAdapter = new EnumToIteratorAdapter(enumProduct.getProduct());
product.displayProduct(enumToIteratorAdapter);
}
}
We didn’t changed anything in either Product Class nor EnumProduct class. We just create a new Adapter class to convert Enumration to Iterator class. Done.
Easy to understand. Thanks
Hi Sir,
I am Rajashekhar, working as java developer in software company, Based on your suggestion , I have purchased Java concurrency 7 cook book. This is good book to know about the java concurrency. Thank you so much for that.
Could you please suggest me good book for both java 7 and j2ee design patterns.It should be explained in Java program only instead of java Applet or Swing.
Please do needful. Thank you so much well in advance.
Thanks and Regards,
Rajashekhar.
very informative and easily understandable blog Sir
Good Explanation Sir,
Thanks for the Article
What is Adaptor
Adaptor is used to make tow parties talk with each otherwise could not talk as their communication channel is not compatible with each other
For Example :
Adaptor for a mobile charger : Mobile can not be directly plugged in to electric socket to charge it . It between it requires an adaptor to to get connected. Adaptor converts 220V from electric socket to 9V what Mobile is made to accept .
An interpreter between two people talking two different languages unknown to each other. Interpreter acts as adaptor here.
Let me take you through java code How we can easily implement and use this pattern in java.
A class A has a method m which takes two parameters String ,String But client who wants to invoke that method does not have a pair of strings but has a Map . So how will they talk . We will insert one more class between then which will act as adaptor . It will take input from client ,will covert the Map parameters in String,String pair combinations and will call the class A method m . Thus client and service class A are not talking with each other directly and Adaptor is making their communication possible.
Here is example with java code . I have written are steps in system.out.println statements in methods
Adaptor Design Pattern
http://efectivejava.blogspot.in/2013/09/java-adaptor-design-pattern.html?utm_source=BP_recent
Hi, you will get complete Java example for Fully Singleton Design pattern and Factory Pattern in below link
http://javadiscover.blogspot.in/search/label/Patterns
Very good case study for Class adapter design pattern. Impressed! Any case study for Object adapter design pattern?