|
Article on other languages:
|
In computer programming, the adapter design pattern (often referred to as the wrapper pattern or simply a wrapper) translates one interface for a class into a compatible interface. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients whilst using the original interface. The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms. 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.
StructureThere are two types of adapter patterns:
The object adapter pattern expressed in UML. The adapter hides the adaptee's interface from the client.
The object adapter pattern expressed in LePUS3.
The class adapter pattern expressed in LePUS3
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 - Object Adapter# Python code sample class Target(object): def specific_request(self): return 'Hello Adapter Pattern!' class Adapter(object): def __init__(self, adaptee): self.adaptee = adaptee def request(self): return self.adaptee.specific_request() client = Adapter(Target()) print client.request() Sample - Class Adapter/** * Java code sample */ /* the client class should instantiate adapter objects */ /* by using a reference of this type */ interface Stack<T> { void push (T o); T pop (); T top (); } /* DoubleLinkedList is the adaptee class */ class DList<T> { public void insert (DNode pos, T o) { ... } public void remove (DNode pos) { ... } public void insertHead (T o) { ... } public void insertTail (T o) { ... } public T removeHead () { ... } public T removeTail () { ... } public T getHead () { ... } public T getTail () { ... } } /* Adapt DList class to Stack interface is the adapter class */ class DListImpStack<T> extends DList<T> implements Stack<T> { public void push (T o) { insertTail (o); } public T pop () { return removeTail (); } public T top () { return getTail (); } } External links
|
|||||||||||||||||||||||||||
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net