Opentopia Directory Encyclopedia Tools

Adapter pattern

Encyclopedia : A : AD : ADA : Adapter pattern


In computer programming, the adapter design pattern (sometimes referred to as the wrapper pattern or simply a wrapper) '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 expressed in UML. The adapter hides the adaptee's interface from the client.
Enlarge
The object adapter pattern expressed in UML. The adapter hides the adaptee's interface from the client.

The class adapter pattern expressed in UML.
Enlarge
The class adapter pattern expressed in UML.

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 Adaptor1

/**
* Java code sample
*/

interface Stack

/* DoubleLinkedList */ class DList 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 Object pop ()

public Object top () }

Sample - Object Adapter

/**
* Java code sample
*/

interface Stack

/* DoubleLinkedList */ class DList 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 public void push (Object o)

public Object pop ()

public Object top () }

External links

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: