Adapter Design Pattern

Back To Index

Overview

The Adapter Design Pattern acts as a bridge between two incompatible interfaces. It allows classes with incompatible interfaces to work together by wrapping an existing class with a new interface.

Key Characteristics

Implementation

The following is an example of an Adapter implementation in Java:


interface Target {
    void request();
}

class Adaptee {
    public void specificRequest() {
        System.out.println("Specific request called.");
    }
}

class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

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

public class AdapterDemo {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new Adapter(adaptee);

        target.request();
    }
}
    

When to Use

Advantages

Disadvantages

Back To Index