DEV Community

eidher
eidher

Posted on • Edited on

3

Adapter Pattern

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Alt Text

Participants

  • Target: defines the domain-specific interface that Client uses.
  • Adapter: adapts the interface Adaptee to the Target interface.
  • Adaptee: defines an existing interface that needs adapting.
  • Client: collaborates with objects conforming to the Target interface.

Code

public class Main {

    public static void main(String[] args) {
        Target target = new Adapter();
        target.request();
    }
}

public interface Target {
    void request();
}

public class Adapter implements Target {
    Adaptee adaptee = new Adaptee();

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

public class Adaptee {
    void specificRequest() {
        System.out.println("Called specificRequest()");
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

Called specificRequest()
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay