Adapter

From CSSEMediaWiki
Revision as of 00:26, 7 October 2008 by Elliot Fisher (Talk | contribs)
Jump to: navigation, search

Adapter allows classes with incompatible interfaces work together, by converting the interface of one class into a form that a client expects. Basically you create an Adapter class as a go between to convert messages from one interface to another.

There are two kinds:

  • Object adapter: makes use of a toolkit/library class to implement its functionality
  • Class-based adapter: uses multiple inheritance to make use of another superclass's functionality

Contents

Use when

  • You have an existing class (eg toolkit or library), but its interface doesn't fit in with your system
  • You want to create a reusable class that cooperates with unrelated or unforeseen classes
  • Object adapter only: You need to use several existing subclasses, but you don't want to adapt their interface by subclassing each one. Object adapters can adapt the interface of the parent class.

Structure

From Gang of Four Design Patterns book:

Class based:

AdapterStructureClass.png

Object based:

AdapterStructureObject.png

Target

  • Defines domain specific interface that a Client uses

Client

  • Collaborates with objects that implement the Target interface

Adaptee

  • An existing interface that needs adapting

Adapter

  • Adapts the interface of Adaptee to the Target interface

Clients call operations on an Adapter object. The Adapter calls Adaptee operations to carry out the request.

Example

From the Gang of Four Design Patterns book:

AdapterExample.png

Personal tools