Adapter pattern: Difference between revisions
m robot Adding: uk:Адаптер (шаблон проектування) |
|||
Line 62: | Line 62: | ||
*[http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/dom/ Java Tutorial on the Document Object Model] (uses the adapter pattern) |
*[http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/dom/ Java Tutorial on the Document Object Model] (uses the adapter pattern) |
||
*[http://citeseer.org/cs?q=Adapter+pattern Citations from CiteSeer] |
*[http://citeseer.org/cs?q=Adapter+pattern Citations from CiteSeer] |
||
*[http://www.vincehuston.org/dp/adapter.html Adapter pattern discussion] with 1-page examples (C++ and Java) |
|||
Revision as of 02:10, 6 October 2007
In computer programming, the adapter design pattern (often 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. The adapter is also responsible for handling any logic necessary to transform data into a form that is useful for the consumer. For instance, if multiple boolean values are stored as a single integer but your consumer requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value.
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 the instance of the wrapped object.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/d/d7/ObjectAdapter.png/300px-ObjectAdapter.png)
- 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. Note that when the adapter must adapt multiple adaptees, the Class Adapter pattern cannot be used in languages such as Java that do not support multiple inheritance.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/3/35/ClassAdapter.png/300px-ClassAdapter.png)
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
{
void push (Object o);
Object pop ();
Object top ();
}
/* DoubleLinkedList */
class DList
{
public void insert (DNode pos, Object o) { ... }
public void remove (DNode pos) { ... }
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 ();
}
}
External links
- Description in Portland Pattern Repository's Wiki
- Java Tutorial on the Document Object Model (uses the adapter pattern)
- Citations from CiteSeer
- Adapter pattern discussion with 1-page examples (C++ and Java)