The Adapter Pattern converts the interface of a class into another interface the client expects. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
An example of the Adapter Design Pattern follows:
public class EnumerationIterator implements Iterator {
Enumeration enum;
public EnumerationIterator(Enumeration enum) {
this.enum = enum;
}
public boolean hasNext() {
return enum.hasMoreElements();
}
public Object next() {
return enum.nextElement();
}
public void remove() {
// NOT Supported by Enumeration
throw new UnsupportedOperationException();
}
}
public class AdapterTest {
// Vector class now supports iterators, but still has the
// elements() method which returns an Enumeration
public static void main(String[] args) {
Vector vector = new Vector();
EnumerationIterator ei;
vector.add("Bill"); vector.add("Sam"); vector.add("Frodo");
ei = new EnumerationIterator(vector.elements());
while (ei.hasNext()) {
System.out.println(ei.next());
}
}
}