adapterPattern

Type of the pattern:

Adapter design pattern is a structural design patterns (design pattern that ease the design by identifying a simple way to realize relationships between entities.)

Problem it solves:

Converts the interface of a class into another interface client expects. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

Example:

Very simple example of adapter pattern is adapting legacy code using enumeration to use java.util.Iterator interface. EnumerationAdapter will adapt enumerator to use Iterator interface

adapterPattern

 

 

 

Public class EnumerationIterator implements Iterator{

 

Enumeration enum;

public EnumerationIterator(Enumeration enum){

This.enum = enum;

}

public booleanhasNext(){

Return enum.hasMoreElements();

}

public Object next(){

Return enum.nextElement();

}

 

public void remove(){

Throw new UnsupportedOperationException();

}

}

public class InfoPrinter{

public void print(Iterator iterator){

while(iterator.hasNext()){

Object object = iterator.next();

system.out.println(object.toString());

}

}

}

The spirit of the Adapter pattern is that Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Action classes in struts framework or servlets in plain jsp-servlet application are adapters for contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request

Adapter Pattern

Post navigation


Leave a Reply

Your email address will not be published. Required fields are marked *