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:
- 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. - 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.
- 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.
- 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.