Adapter

Gamma et al. 95

Intent

  • An adapter converts the interface from one class to another in accordance with the client’s expectations.

Motivation

  • How can a class be reused that does not have an interface that a client requires?

  • How can classes that have incompatible interfaces work together?

  • How to provide an alternative interface for a class?

Motivation Example

adapter example

Structure of the object adapter pattern

object adapter

Consequences

  • Respects the Single Responsibility Principle and the Open/Closed Principle

  • Increases the overall complexity of the code

Class and object adapters

There are two main variants of the adapter pattern:

  • The object adapter uses a reference to the class that gets adapted, which means at runtime that there is one adapter object and one adaptee object.

  • The class adapter uses an inheritance to the class that gets adapted, which means at runtime that there is a single object.

The previous slides have only presented the most common variant, ie. the object adapter.

Structure of the class adapter pattern

class adapter

Implementation tradeoffs

Class and object adapters have different trade-offs.

  • A class adapter:

  • adapts the Adaptee to the Target by committing to a concrete Adaptee class. As a consequence, a class adapter won’t work when we want to adapt a class and all its subclasses.

  • lets the Adapter override some of the Adaptee’s behavior, since Adapter is a subclass of Adaptee.

  • introduces only one object, and no additional pointer indirection is needed to get to the Adaptee.

Implementation tradeoffs (Cont.)

  • An object adapter:

  • lets a single Adapter work with many *Adaptee*s—that is, the Adaptee itself and all of its subclasses (if any). The Adapter can also add functionality to all Adaptees at once.

  • makes it harder to override the Adaptee behavior. It will require subclassing Adaptee and making Adapter refer to the subclass rather than the Adaptee itself.

Author and Date

  • Design Patterns: Elements of Reusable Object-Oriented Software. Erich Gamma, Richard Helm,Ralph Johnson, and John Vlissides. Addison Wesley. October 1994.

More Examples

  • java.util.Arrays#asList()

  • java.util.Collections#list()

  • java.util.Collections#enumeration()

  • javax.xml.bind.annotation.adapters.XML*Adapter*

Back to main presentation