Skip to content Skip to navigation

Connexions

You are here: Home » Content » Adapter Design Pattern

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

Adapter Design Pattern

Module by: Stephen Wong

Summary: A brief description of the Adapter Design Pattern

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 type in the following discussion
Definition 1: Adapters
Adapters are used to enable objects with different interfaces to communicate with each other.

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.

Object Adapters

Object adapters use a compositional technique to adapt one interface to another.    The adapter inherits the target interface that the client expects to see, while it holds an instance the adaptee.   When the client calls the request() method on its target object (the adapter), the request is translated into the corresponding specific request on the adaptee.

Figure 1: Object adapter implementation of the Adapter Design Pattern.
Figure 1 (graphics1.png)

Object adapters enable the client and the adaptee to be completely decoupled from each other.   Only the adapter knows about both of them.

Class Adapters

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.

Figure 2: Class adapter implementation of the Adapter Design Pattern.
Figure 2 (graphics2.png)

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.

Comparison Between Object and Class Adapters

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.

References

  1. Gamma, Helm, Johnson and Vlissides. (1995). Design Patterns, Elements Of Reusable Object-Oriented Software. Reading, MA: Addison-Wesley.

Comments, questions, feedback, criticisms?

Send feedback