tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Design Patterns > Java Design Patterns > Adapter

Adapter 

Adapter pattern comes under Structural design pattern. Converts the interface of one class into another interface that client expects. It is also know as wrapper pattern.

Behaviour & Advantages

  • Lets classes work together that couldn't otherwise because of incompatible interfaces.
  • Wraps the existing functionality with new functionality.
  • Wrapped implementation is completely hidden from the client.
Participants
  • Target
    New abstract interface that client is expecting.
  • Adapter
    An implementation of Target by wrapping the functionality of Adaptee. Adapter may wrap the Adaptee functionality either by inheritance (extend Adaptee) or Containership (Keep a reference of Adaptee in Adapter and deligate calls as needed). It maps the client interface to the adaptee interface.
  • Adaptee
    Existing functionality or legacy code that needs adapting.
  • Client
    One who uses new Target interface.

This example shows a simple stack implementation from legacy Vector class. Here interface IStack acts as Target interface and StackAdapter acts as Adapter which wraps the functionality of Vector (Adaptee).

The stack interface is shown below,

File Name  :  
com/bethecoder/tutorials/dp/adapter/IStack.java 
   
package com.bethecoder.tutorials.dp.adapter;

public interface IStack<E> {
  
  public void push(E ele);
  public boolean empty();
  public E pop();
  public E peek();
  
}
   

Stack Adapter uses containership to wrap the functionality of Vector (Adaptee).

File Name  :  
com/bethecoder/tutorials/dp/adapter/StackAdapter.java 
   
package com.bethecoder.tutorials.dp.adapter;

import java.util.Vector;

public class StackAdapter<E> implements IStack<E> {

  /**
   * Adapt legacy vector to IStack.
   */
  private Vector<E> items = new Vector<E>();
  
  @Override
  public void push(E ele) {
    items.addElement(ele);
  }
  
  @Override
  public boolean empty() {
    return items.size() == 0;
  }
  
  @Override
  public E peek() {
    verify();
    return items.lastElement();
  }

  @Override
  public E pop() {
    verify();
    E topEle = peek();
    items.removeElement(topEle);
    return topEle;
  }

  private void verify() {
    if (items.size() == 0) {
      throw new RuntimeException("Empty Stack !!");
    }
  }
  
  public String toString() {
    return "StackAdapter" + items;
  }
}
   

Adapter usage is shown below,

File Name  :  
com/bethecoder/tutorials/dp/adapter/Test.java 
   
package com.bethecoder.tutorials.dp.adapter;

public class Test {

  /**
   @param args
   */
  public static void main(String[] args) {

    IStack<String> stack = new StackAdapter<String>();
    stack.push("ONE");
    stack.push("TWO");
    stack.push("THREE");
    stack.push("FOUR");
    stack.push("FIVE");
    stack.push("SIX");
    
    System.out.println(stack);
    
    while (!stack.empty()) {
      System.out.println("->" + stack.pop());
    }
  }

}
   

It gives the following output,
StackAdapter[ONE, TWO, THREE, FOUR, FIVE, SIX]
->SIX
->FIVE
->FOUR
->THREE
->TWO
->ONE



 
  


  
bl  br