Citizendia
Your Ad Here

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. In Software engineering, a design pattern is a general reusable solution to a commonly occurring problem in Software design. Interface generally refers to an abstraction that an entity provides of itself to the outside In Object-oriented programming, a class is a Programming language construct that is used as a blueprint to create objects This blueprint includes attributes 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.

Contents

Structure

There are two types of adapter patterns:

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. In its simplest embodiment an object is an allocated region of storage
The object adapter pattern expressed in UML. The adapter hides the adaptee's interface from the client.
The object adapter pattern expressed in UML. Unified Modeling Language ( UML) is a standardized general-purpose Modeling language in the field of Software engineering. The adapter hides the adaptee's interface from the client.
The object adapter pattern expressed in LePUS3.
The object adapter pattern expressed in LePUS3. LePUS3 is an Object-oriented, visual Design Description Language namely a software Modelling language and a Formal specification language that is suitable


Class Adapter pattern 
This type of adapter uses multiple inheritance to achieve its goal. Multiple inheritance refers to a feature of some object-oriented Programming languages in which a class can inherit behaviors and features from 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. A programming language is an Artificial language that can be used to write programs which control the behavior of a machine particularly a Computer.
The class adapter pattern expressed in UML.
The class adapter pattern expressed in UML.
The class 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. LePUS3 is an Object-oriented, visual Design Description Language namely a software Modelling language and a Formal specification language that is suitable In the context of Enterprise architecture, Service-orientation, and Service-oriented architecture, the term service refers to a discretely defined set of Interface generally refers to an abstraction that an entity provides of itself to the outside 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. The Document Object Model ( DOM) is a platform- and language -independent standard Object model for representing HTML or XML and related Don't change "Extensible" 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


© 2009 citizendia.org; parts available under the terms of GNU Free Documentation License, from http://en.wikipedia.org
Dapyx Software network: MP3 Explorer | Ebook Manager | Zenithic