Adapter

Intent

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Class diagram of the original GoF pattern

Class adapter

structure-GoF:adapter1

Object adapter

structure-GoF:adapter2

Another solution in MixJuice

Solved Problem(s)

Extensibility problem
Object adapters make it impossible to override Adaptee behavior without modifying the source code of Adapter. It requires new subclass SubAdaptee and modifying Adapter so that it create an instance of SubAdaptee instead of Adaptee. (This problem is mentioned at p.142 lines 14-16 in the GoF book.)
Complexity problem
In order to adapt a class and all its subclasses using class adapters, each class to be adapted requires corresponding class adapter. (This problem is mentioned at p.140 lines 24-26 and p.143 lines 9-12 in the GoF book.)

Used programming technique(s)

Super-interface addition to Adaptee class.

Consequences

The followings are trade-offs between class adapters, object adapters and this solution. (See also p.142 in the GoF book that explains trade-offs between class and object adapters.)

Structure and pseudo code

structure:adapter_sample1

module library {
  define class Adaptee {
    define void specificRequest() {...}
  }
  define class SubAdaptee extends Adaptee {...}
}
module application extends library {
  define interface Target {
    define void request();
  }
  class Adaptee implements Target {
    void request() { specificRequest(); }
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top