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.

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
6 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
RinkuSatya MaheshRajashekharagouda PatilCarolmanoj Recent comment authors
newest oldest most voted
Carol
Guest
Carol

Easy to understand. Thanks

Jack
Guest
Jack

Very good case study for Class adapter design pattern. Impressed! Any case study for Object adapter design pattern?

Anand
Guest
Anand

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

manoj
Guest
manoj

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… Read more »

Satya Mahesh
Guest
Satya Mahesh

Good Explanation Sir,
Thanks for the Article

Rinku
Guest
Rinku

very informative and easily understandable blog Sir