AlgoViz

Adapter pattern

easy 50 min

The Adapter pattern makes an existing class fit the interface that its client expects, without changing either of them.

Watch lesson4:39

Chapter mnemonic

Structural design patterns

Ancient books conceal dragons folded from paper.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

Is what they want, has what exists.

The hose coupling: claws on one side, screw thread on the other, and a dial that turns litres into strokes. Pump and fire engine stay as they are.

Cheat sheet

Parts
Target, client, adaptee, adapter
Default form
Object adapter
New library
One class, one line of wiring
New promise
Touches every adapter
Two errors
No, and unknown
Proof
One contract suite for all

Spot it when you see

  • A library or vendor with its own words
  • Several providers behind one feature
  • Old code that must stay
  • Error codes

Tricks that stick

  • ADAPT: Adapts interfaces, Delegates to the adaptee, Allows reuse, Provides compatibility, Translates requests.
  • NOUTEE for the gap list: Names, Order, Units, Types, Errors, Empty.
  • Changed and unplanned: adapter. Changed and planned: bridge.

Do not forget

  • A target copied from the library
  • Ignored return codes
  • Business rules inside
  • Vendor types as results
  • Timeout reported as no
  • Adapters around adapters