Adapter Pattern Article Index for
Adapter
Website Links For
Adapter
 

Information About

Adapter Pattern




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 the designers of these languages consider it a dangerous practice.




The adapter pattern is useful in situations where an already existing class provides some or all of the Service s 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 ADAPTOR1

  • ---

  • Java code sample

  • /


interface Stack
{
void push (Object);
Object pop ();
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

  • /


interface Stack
{
void push (Object o);
Object pop ();
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 implements 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