The Adapter Design Pattern

The Adapter design pattern is a structural design pattern.

What is structural design pattern ?

Structural Design Patterns help in building the structure of a software system. They focus on how objects and classes work together to form a bigger system.

Adapter design pattern is one of the structural design pattern which helps two incompatible systems to work together without modifying their existing code. It acts like a bridge between them.

Example:

A system currently receives and processes data in XML format. However, a new third-party library only supports JSON. Instead of modifying the entire system, we use the Adapter Pattern to convert XML to JSON on the fly.

package DesignPatterns.structural.adapter.xmltojsonadapter;

// Existing system that processes XML
public class XMLProcessor {
    public void processXMLData(String xmlData) {
        System.out.println("Processing XML data: " + xmlData);
    }
}
package DesignPatterns.structural.adapter.xmltojsonadapter;

// New system that processes JSON
public class JSONProcessor {
    public void processJSONData(String jsonData) {
        System.out.println("Processing JSON data: " + jsonData);
    }
}
package DesignPatterns.structural.adapter.xmltojsonadapter;

// Adapter: Converts XML to JSON before processing
public class XmlToJsonAdapter {
    private JSONProcessor jsonProcessor;

    public XmlToJsonAdapter(JSONProcessor jsonProcessor) {
        this.jsonProcessor = jsonProcessor;
    }

    public void processXMLData(String xmlData) {
        System.out.println("Adapter: Converting XML to JSON...");

        // Convert XML to JSON
        String jsonData = convertXMLToJSON(xmlData);

        // Process JSON using the JSONProcessor
        jsonProcessor.processJSONData(jsonData);
    }

    private String convertXMLToJSON(String xmlData) {
        System.out.println("Converting XML to JSON...");
        // Example logic (In real cases, use a library like org.json or Jackson)
        return "{ \"converted\": \"" + xmlData + "\" }";
    }
}
package DesignPatterns.structural.adapter.xmltojsonadapter;

public class Client {
    public static void main(String[] args) {
        String xmlData = "<user><name>Nivedita</name></user>";
        JSONProcessor jsonProcessor = new JSONProcessor();

        // Use Adapter to convert and process data
        XmlToJsonAdapter adapter = new XmlToJsonAdapter(jsonProcessor);

        adapter.processXMLData(xmlData);
    }
}

Why Use Adapter Here?

  1. The existing system expects XML, but the new library uses JSON.

  2. We don’t modify either the XMLProcessor or JSONProcessor.

  3. Adapter acts as a bridge, converting XML to JSON before processing.

Pros and Cons of the Adapter Design Pattern

AspectProsCons
CompatibilityAllows incompatible interfaces to work together.Adds extra complexity to the system.
EncapsulationKeeps conversion logic separate from business logic.Requires additional classes, increasing code size.
Code ReusabilityEnables reuse of existing classes without modification.Can make debugging harder due to indirect method calls.
MaintainabilityMakes it easier to integrate third-party libraries.May become overcomplicated if overused.
ScalabilityNew adapters can be created without modifying the core system.If multiple adapters are needed, it adds many extra classes.
PerformanceHelps bridge old and new systems efficiently.Performance overhead due to format conversion (e.g., XML → JSON).
Design PrinciplesFollows Open/Closed Principle (OCP).Violates Single Responsibility Principle (SRP) if the adapter does too much.