Friday, January 23, 2009

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.



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.

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mycompany.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<String> eProduct = product.elements();
return eProduct;
}
}

Here is Company A “Product” program which is use to display all products on console screen.

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.mycompany.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

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.mycompany.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!!!

1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.mycompany.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.

No comments: