Java Language – 84 – Adapter

Design Patterns – Adapter
Introduction to the Adapter Pattern

The Adapter pattern is a structural design pattern used in Java to enable the interface of an existing class to be used as another interface. It allows incompatible interfaces to work together, making objects work with others that they otherwise couldn’t. This pattern acts as a bridge between two interfaces, allowing them to collaborate seamlessly.

Why Use the Adapter Pattern?

The Adapter pattern offers several advantages, making it a valuable design pattern:

  • Compatibility: It enables objects with incompatible interfaces to work together, promoting reusability.
  • Client Independence: It isolates the client code from the complexities of the adapted class, enhancing maintainability.
  • Seamless Integration: It provides a straightforward way to incorporate legacy code into new systems.
Implementing the Adapter Pattern

The Adapter pattern can be implemented in Java by creating an adapter class that implements the target interface and delegates calls to the adapted class. Let’s take a look at an example:


// Target interface
public interface Target {
    void request();
}

// Adaptee class (incompatible interface)
public class Adaptee {
    public void specificRequest() {
        System.out.println("Adaptee's specific request.");
    }
}

// Adapter class
public class Adapter implements Target {
    private Adaptee adaptee;

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

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

In the example above, we have a target interface called Target and an incompatible class Adaptee. The Adapter class implements the Target interface and contains an instance of the Adaptee. It delegates the request() method to the specificRequest() method of the Adaptee.

Usage of the Adapter Pattern

The Adapter pattern is commonly used when you need to make an existing class work with an interface it was not designed to support. It acts as an intermediary, translating requests from one interface to another.

Types of Adapters

There are two types of adapters: class adapters and object adapters. In the class adapter, the adapter class extends the adaptee class. In the object adapter, the adapter class contains an instance of the adaptee class. The example provided demonstrates the object adapter approach.

Comparison with the Bridge Pattern

The Adapter pattern is often confused with the Bridge pattern, as both deal with abstraction and implementation. However, they serve different purposes. The Adapter pattern focuses on making existing classes work with incompatible interfaces, while the Bridge pattern is about separating an object’s abstraction from its implementation.

Conclusion

The Adapter pattern is a valuable design pattern in Java for achieving compatibility between interfaces that would otherwise be incompatible. It simplifies the integration of legacy code and enhances code reusability. By using the Adapter pattern, you can connect objects with different interfaces seamlessly and isolate clients from the complexities of adapted classes, resulting in maintainable and extensible code.