Card 08/ 08

RecapDifficulty: Advanced1 min

Recap: Adapter Pattern

Translate one interface into another without touching either side.

Adapter lets a client use an existing class whose interface does not fit, by putting a translating class between them.

  • Intent. Convert the interface of a class into another interface clients expect, so incompatible classes can work together.
  • Participants. The target is the client's interface, the adaptee is the existing class, and the adapter implements the target and calls the adaptee.
  • Object or class adapter. The object adapter holds the adaptee and is the usual form; the class adapter inherits from it.
  • Recognise it by shape. A class that implements one interface and forwards each call, translated, to an object with another.
  • In the wild. InputStreamReader is a Reader made from an InputStream; Arrays.asList is a List over an array, which cannot grow.
  • The trap. An adapter only translates. A rule that would still be needed without the mismatch belongs outside it.