<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id14555525">
  <name>Adapter Design Pattern</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2007/05/16 14:59:09.935 GMT-5</md:created>
  <md:revised>2007/06/22 13:18:27.484 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>adapter</md:keyword>
    <md:keyword>design pattern</md:keyword>
    <md:keyword>Java</md:keyword>
  </md:keywordlist>

  <md:abstract>A brief description of the Adapter Design Pattern</md:abstract>
</metadata>
  <content>

      <note>Do not confuse the abstract concept of an "interface" (the set of methods/behaviors that one object expects to see when communicating with another object) with the Java interface <emphasis>type</emphasis>  in the following discussion</note>

        <definition id="def-1">
<term>Adapters</term>
<meaning>Adapters are used to enable objects with different interfaces to communicate with each other.</meaning></definition>
        <para id="id17355368">Adapters come in two flavors, object adapters and class adapters.  These two methods embody the difference between the use of inheritance and composition to solve problems.</para>
      <section id="id-762085357564">
        <name>Object Adapters</name>
        <para id="id17951952">Object adapters use a compositional technique to adapt one interface to another.    The <term>adapter</term> inherits the <term>target</term> interface that the <term>client</term> expects to see, while it holds an instance the <term>adaptee</term>.   When the client calls the <code>request()</code> method on its target object (the adapter), the request is translated into the corresponding specific request on the adaptee. </para>
        <figure id="id17401904"><media type="image/png" src="graphics1.png">
	</media>
<caption> Object adapter implementation of the Adapter Design Pattern.
</caption>
</figure>
        <para id="id15556878">Object adapters enable the client and the adaptee to be completely decoupled from each other.   Only the adapter knows about both of them. </para>
      </section>
      <section id="id-159402123054">
        <name>Class Adapters</name>
        <para id="id16713753">Class adapters use multiple inheritance to achieve their goals.   As in the object adapter, the class adapter inherits the interface of the client's target.   However, it also inherits the interface of the adaptee as well.   Since Java does not support true multiple inheritance, this means that one of the interfaces must be inherited from a Java Interface type.   Note that either or both of the target or adaptee interfaces could be Java Interfaces.   The request to the target is simply rerouted to the specific request that was inherited fro the adaptee interface.</para>
        <figure id="id3637131"><media type="image/png" src="graphics2.png">
	</media>
<caption> Class adapter implementation of the Adapter Design Pattern.
</caption>
</figure>
        <para id="id18344684">Note that class adapters have a problem with name conflicts if methods of the same signature exist on both the target and the adaptee.  Note that just because two objects have methods that have the same signature (syntax), it does not guarantee that the two methods have the same meaning or behavior (sematics).    That is, the two methods do not necessarily map directly to each other.   Object adapters do not have this problem.</para>
      </section>
      <section id="id-952088236256">
        <name>Comparison Between Object and Class Adapters</name>
        <para id="id6476475">Class adapters are simpler than object adapters in that they involve fewer classes and are useful if total decoupling of the client and adaptee is not needed. Object adapters give better decoupling and are thus more flexible. </para>
      </section>




  </content>
<bib:file>
  <bib:entry id="gof">
    <bib:book>
      <bib:author>Gamma, Helm, Johnson and Vlissides</bib:author>
      <bib:title>Design Patterns, Elements Of Reusable Object-Oriented Software</bib:title>
      <bib:publisher>Addison-Wesley</bib:publisher>
      <bib:year>1995</bib:year>
<bib:address>Reading, MA</bib:address>
    </bib:book>
  </bib:entry>

</bib:file></document>
