Skip to content
BENIBIX Learn

Adapter Pattern

Development🏗️ ArchitectureDesign Patterns

Also known as Adapter, Wrapper Pattern

A wrapper that lets one piece of code fit another.

Think of it like

Think of it like a travel plug. Your charger stays the same, and the plug makes it fit the wall.

Example

Jack's app expects a send method. The new text library has a postText method. A small adapter class turns one into the other.

Why it matters

It lets you use outside code without letting its shape spread through your whole app.

Definition

The adapter pattern wraps one piece of code so it looks like another. It is used when two parts should work together but expect different shapes. The adapter sits in between and turns each call into the form the other side needs. Neither side has to change. It is common when you plug in an outside library or an old system.

Common mistake

Letting an outside library's own types leak into every file. Wrap it once, and a later switch touches one place.

Key takeaways

  1. 1
    Wrap and translate. One shape into another.
  2. 2
    Neither side changes. The adapter does the work.
  3. 3
    Guard the border. Around outside code.

Compare