Adapter pattern

In computer programming, the adapter design pattern 'adapts' one interface for a class into one that a client expects. An adapter allows classes to work together that normally could not because of incompatible interfaces by wrapping its own interface around that of an already existing class.

There are two types of adapter patterns:

  • The Object Adapter pattern - In this type of adapter pattern the adapter contains an instance of the class it wraps. In this situation the adapter makes calls to a physical instance of the wrapped object.
  • The Class Adapter pattern - This type of adapter uses multiple inheritance to achieve its goal. The adapter is created inheriting interfaces from both the interface that is expected and the interface that is pre-existing. The Object Adapter pattern is more often used as some popular languages, such as Java, do not support true multiple inheritance as it is generally thought of as bad practice.

The adapter pattern is useful in situations where an already existing class provides some or all of the services you need but does not use the interface you need. A good real life example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed. A link to a tutorial that uses the adapter design pattern is listed in the links below.

Sample - Class Adapter

/**
 * Java code sample 
 */

interface Stack
{
  public void push (Object);
  public Object pop ();
  public Object top ();
}

/* DoubleLinkedList */
class DList
{
  public void insert (DNode pos, Object o) { ... }
  public void remove (DNode pos, Object o) { ... }
  
  public void insertHead (Object o) { ... }
  public void insertTail (Object o) { ... }
  
  public Object removeHead () { ... }
  public Object removeTail () { ... }
  
  public Object getHead () { ... }
  public Object getTail () { ... }
}

/* Adapt DList class to Stack interface */
class DListImpStack extends DList implements Stack
{
  public void push (Object o) {
    insertTail (o);
  }
  
  public Object pop () {
    return removeTail ();
  }
  
  public Object top () {
    return getTail ();
  }
}

Sample - Object Adapter

/**
 * Java code sample 
 */

class Stack
{ public Stack() { ... }
  public void push (Object o) { ... }
  public Object pop () { ... }
  public Object top () { ... }
}

/* DoubleLinkedList */
class DList
{
  public void insert (DNode pos, Object o) { ... }
  public void remove (DNode pos, Object o) { ... }
  
  public void insertHead (Object o) { ... }
  public void insertTail (Object o) { ... }
  
  public Object removeHead () { ... }
  public Object removeTail () { ... }
  
  public Object getHead () { ... }
  public Object getTail () { ... }
}

/* Adapt DList class to Stack interface */
class DListStack extends Stack 
{ 
  private DList _dlist;
  public DListStack() { _dlist = new DList()}
  public void push (Object o) {
    _dlist.insertTail (o);
  }
  
  public Object pop () {
    return _dlist.removeTail ();
  }
  
  public Object top () {
    return _dlist.getTail ();
  }
}

External links

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools