skipnothing/Design Patterns

Adapter

Your whole app calls notify.send(msg). The SMS vendor you just signed names the same action dispatch, wants a recipient and an options bag, and there are two hundred call sites between you and a library you can't edit.

  • ▸Swap which object you hand the call sites and all two hundred stay untouched, not a character changes.
  • ▸Expose the interface you wrapped and you've added behaviour; expose a different one and you've translated, that's the pattern.
  • ▸Arrays.asList and InputStreamReader are this shipped inside the platform, the array-to-list and bytes-to-characters conversion is already written for you.
01

Two signatures that won't line up

Your code sends notifications the same way everywhere:

notify.send("Your order shipped")   // one string, that's the whole contract

Two hundred call sites across the app lean on that one-string shape. Now you sign an SMS vendor, and its SDK has its own idea of how a message gets sent:

client.dispatch("+15551234567", "Your order shipped", { retries: 2 })

Same action, different shape. Yours takes one argument; theirs takes three, in a different order, with a recipient and an options object yours never had. The two signatures don't line up, and you can't make them.

You can't change the vendor's dispatch, you didn't write it and it ships as a compiled dependency. You won't change all two hundred call sites either, that's two hundred edits to chase one new library, and the next vendor would mean two hundred more. Both sides are fixed. The mismatch sits in the middle, and neither end will move to close it.

THE MISMATCH IN THE MIDDLE

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 18+ more topics in Design Patterns.

Sign up, it's freeSee the full Design Patterns
Structural Patterns0/5#8 Composite
#10 Proxy