Adapter Design Pattern in Java

1. Definition

The Adapter Design Pattern is a structural pattern that allows objects with incompatible interfaces to collaborate. It acts as a bridge between two incompatible interfaces by converting the interface of one class into an interface expected by the client.

2. Problem Statement

Imagine you have two systems or libraries with incompatible interfaces. You want them to work together, but their interfaces don't match, making direct communication impossible.

3. Solution

The Adapter pattern provides a solution by introducing an intermediary interface (an adapter) that can make one interface look like another. By doing so, it allows code components with incompatible interfaces to communicate.

4. Real-World Use Cases

1. Power adapters that let you use a device from one country in another country's outlet type.

2. Memory card adapters that allow a microSD card to fit into an SD card slot.

3. Software drivers that enable applications to work with new or different hardware.

5. Implementation Steps

1. Identify the existing interfaces that are incompatible.

2. Create an adapter class that bridges the two interfaces.

3. The adapter class implements one interface and receives an instance of the object with the other interface.

6. Implementation

// Step 1: Define the target interface.
interface Target {
    void request();
}

// Step 2: Implement an adapter class.
class Adapter implements Target {
    private Adaptee adaptee;

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

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

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

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

        target.request();
    }
}

Output:

Called specificRequest() of Adaptee.

Explanation

In the example:

1. The Target interface represents the expected interface.

2. Adaptee is an existing class with a different interface.

3. Adapter is the class that implements the Target interface and translates calls to it into calls on the Adaptee.

4. In the demonstration, the client calls request() on the Target interface. The adapter translates this call to specificRequest() on the Adaptee.

Thus, the client can interact with Adaptee through the adapter without knowing about Adaptee.

7. When to use?

1. When you want to use an existing class, but its interface doesn't match the one you need.

2. When you want to create a reusable class that works with classes that may not yet exist or are incompatible.

3. When you need to adapt multiple existing subclasses but don't want to modify each of them (the adapter can interface with each subclass).

The Adapter Design Pattern is all about creating an intermediary that translates or adapts one interface to another, making system integration smooth.


Comments