0
0
LldConceptBeginner · 3 min read

Adapter Pattern: Definition, Example, and When to Use

The Adapter Pattern is a design pattern that allows incompatible interfaces to work together by wrapping one interface with another. It acts like a translator between two objects so they can communicate without changing their existing code.
⚙️

How It Works

Imagine you have a phone charger that fits only a certain type of socket, but you want to use it in a different socket type. Instead of changing the charger or the socket, you use an adapter that connects the charger to the socket. The adapter changes the shape of the plug so they fit together.

In software, the Adapter Pattern works the same way. It takes an object with one interface and wraps it with another interface that the client expects. This way, two incompatible parts can work together without changing their original code.

The adapter acts as a middleman that converts calls from the client into calls the wrapped object understands.

💻

Example

This example shows a simple adapter that allows a SquarePeg to fit into a RoundHole by adapting the interface.

javascript
class RoundHole {
    constructor(radius) {
        this.radius = radius;
    }
    fits(peg) {
        return peg.getRadius() <= this.radius;
    }
}

class RoundPeg {
    constructor(radius) {
        this.radius = radius;
    }
    getRadius() {
        return this.radius;
    }
}

class SquarePeg {
    constructor(width) {
        this.width = width;
    }
    getWidth() {
        return this.width;
    }
}

class SquarePegAdapter {
    constructor(squarePeg) {
        this.squarePeg = squarePeg;
    }
    getRadius() {
        // Calculate the radius of the smallest circle that fits the square peg
        return this.squarePeg.getWidth() * Math.sqrt(2) / 2;
    }
}

const hole = new RoundHole(5);
const roundPeg = new RoundPeg(5);
console.log(hole.fits(roundPeg)); // true

const smallSquarePeg = new SquarePeg(5);
const largeSquarePeg = new SquarePeg(10);
const smallSquarePegAdapter = new SquarePegAdapter(smallSquarePeg);
const largeSquarePegAdapter = new SquarePegAdapter(largeSquarePeg);

console.log(hole.fits(smallSquarePegAdapter)); // true
console.log(hole.fits(largeSquarePegAdapter)); // false
Output
true true false
🎯

When to Use

Use the Adapter Pattern when you want to reuse existing code but the interfaces do not match. It helps when integrating third-party libraries or legacy code that cannot be changed.

For example, if you have a new system that expects data in one format but an old system provides it in another, an adapter can convert between these formats without rewriting either system.

It is also useful when you want to create a common interface for different classes that have incompatible interfaces.

Key Points

  • The Adapter Pattern allows incompatible interfaces to work together.
  • It wraps an existing object to provide a new interface.
  • It promotes code reuse without modifying existing code.
  • Commonly used when integrating legacy or third-party code.
  • Acts like a translator or connector between two systems.

Key Takeaways

The Adapter Pattern lets incompatible interfaces work together by wrapping one with another.
It helps reuse existing code without changing it by acting as a translator.
Use it when integrating legacy systems or third-party libraries with different interfaces.
The adapter converts calls from the client to the wrapped object's interface.
It promotes flexibility and easier maintenance in software design.