Design Pattern

Adapter design pattern

The Adapter design pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces by creating a class that wraps one interface and exposes another interface that the client expects.

Here’s a practical example in Java:

Suppose we have a third-party library that provides a XMLParser class for parsing XML documents, but our application requires a different interface, let’s call it Parser. We can create an adapter class that implements the Parser interface and uses the XMLParser internally.

// Third-party library class
class XMLParser {
    public String parseXML(String xmlData) {
        // Parse XML data and return the result
        return "Parsed XML data";
    }
}

// Our application's interface
interface Parser {
    String parse(String data);
}

// Adapter class
class XMLParserAdapter implements Parser {
    private XMLParser xmlParser;

    public XMLParserAdapter() {
        xmlParser = new XMLParser();
    }

    @Override
    public String parse(String data) {
        return xmlParser.parseXML(data);
    }
}

// Client code
class Client {
    public static void main(String[] args) {
        Parser parser = new XMLParserAdapter();
        String data = "<data>...</data>";
        String result = parser.parse(data);
        System.out.println(result);
    }
}

In this example, the XMLParserAdapter class adapts the XMLParser interface to the Parser interface expected by our application. The client code can now work with the Parser interface without knowing the details of the XMLParser implementation.

Industry examples:

  1. Java I/O Adapters: Java provides various reader and writer classes for different data sources and destinations (e.g., FileReader, BufferedReader, InputStreamReader). These classes act as adapters, allowing applications to read and write data from different sources and destinations using a common interface.
  2. Database Adapters: When working with different database management systems (DBMS), adapters are often used to provide a common interface for executing queries and retrieving data. For example, the JDBC (Java Database Connectivity) API provides a standardized interface for interacting with different types of databases, while database-specific JDBC drivers act as adapters, translating the JDBC calls into database-specific commands.
  3. Web Service Adapters: When integrating with external web services that have different interfaces, adapters can be used to provide a consistent interface for the client application. For example, an adapter can translate the client’s method calls into the corresponding web service requests and responses.
  4. GUI Toolkits: In GUI development, adapters are commonly used to bridge the gap between UI components and application logic. For example, the MouseAdapter in Java Swing allows developers to extend and override only the necessary mouse event methods, providing a convenient way to handle mouse events in GUI applications.

The Adapter pattern is widely used in various domains to integrate different systems, libraries, or components that were not initially designed to work together, promoting code reusability and maintainability.

Avatar

Neelabh

About Author

As Neelabh Singh, I am a Senior Software Engineer with 6.6 years of experience, specializing in Java technologies, Microservices, AWS, Algorithms, and Data Structures. I am also a technology blogger and an active participant in several online coding communities.

You may also like

Blog Design Pattern

Understanding the Builder Design Pattern in Java | Creational Design Patterns | CodeTechSummit

Overview The Builder design pattern is a creational pattern used to construct a complex object step by step. It separates
Blog Design Pattern Spring Boot

Mastering Dependency Injection and Inversion of Control: A Comprehensive Guide

Inversion of Control (IoC) is a design principle in which the control flow of a program is inverted: instead of the