0
0
JavaConceptBeginner · 3 min read

Adapter Pattern in Java: What It Is and How It Works

The Adapter Pattern in Java is a design pattern that allows incompatible interfaces to work together by wrapping an existing class with a new interface. It acts like a bridge, converting the interface of one class into another that clients expect.
⚙️

How It Works

Imagine you have a device with a plug that doesn't fit your wall socket. You use an adapter to connect the device to the socket without changing the device or the socket. Similarly, the Adapter Pattern in Java wraps an existing class with a new interface so that it can work with other classes that expect a different interface.

This pattern involves three parts: the client (which wants to use a certain interface), the adapter (which converts one interface to another), and the adaptee (the existing class with an incompatible interface). The adapter implements the interface expected by the client and internally calls the adaptee's methods, translating calls as needed.

💻

Example

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

java
interface Round {
    double getRadius();
}

class RoundHole {
    private double radius;
    public RoundHole(double radius) {
        this.radius = radius;
    }
    public boolean fits(Round peg) {
        return peg.getRadius() <= radius;
    }
}

class SquarePeg {
    private double width;
    public SquarePeg(double width) {
        this.width = width;
    }
    public double getWidth() {
        return width;
    }
}

// Adapter class
class SquarePegAdapter implements Round {
    private SquarePeg peg;
    public SquarePegAdapter(SquarePeg peg) {
        this.peg = peg;
    }
    public double getRadius() {
        // Calculate radius of the smallest circle that fits the square peg
        return peg.getWidth() * Math.sqrt(2) / 2;
    }
}

public class AdapterPatternDemo {
    public static void main(String[] args) {
        RoundHole hole = new RoundHole(5);
        SquarePeg smallPeg = new SquarePeg(5);
        SquarePeg largePeg = new SquarePeg(10);

        SquarePegAdapter smallPegAdapter = new SquarePegAdapter(smallPeg);
        SquarePegAdapter largePegAdapter = new SquarePegAdapter(largePeg);

        System.out.println("Small square peg fits: " + hole.fits(smallPegAdapter));
        System.out.println("Large square peg fits: " + hole.fits(largePegAdapter));
    }
}
Output
Small square peg fits: true Large square peg fits: false
🎯

When to Use

Use the Adapter Pattern when you want to use an existing class but its interface does not match what your code expects. It helps integrate new or legacy code without changing existing classes.

For example, if you have a library with a different interface than your application, an adapter can make them work together smoothly. It is also useful when you want to reuse classes that cannot be modified.

Key Points

  • The adapter acts as a bridge between two incompatible interfaces.
  • It allows reuse of existing classes without modifying them.
  • Helps integrate legacy or third-party code.
  • In Java, adapters often implement the target interface and hold a reference to the adaptee.

Key Takeaways

The Adapter Pattern lets incompatible interfaces work together by wrapping one with another.
It is useful for integrating legacy or third-party code without changes.
Adapters implement the expected interface and translate calls to the adaptee.
Use it when you want to reuse existing classes with different interfaces.