DEV Community

Vivek Kurmi
Vivek Kurmi

Posted on • Updated on

Adapter Design Pattern step by step

The Adapter Design Pattern is a structural design pattern that allows objects with incompatible interfaces to work together.
It acts as a bridge between two incompatible interfaces, making them compatible without changing their source code.
This pattern is particularly useful when integrating new features or libraries into existing code or when you have to work with legacy systems.

In software development, the Adapter design pattern is like a language translator that helps two incompatible systems or classes work together smoothly.
Let's illustrate this concept with a coding example:

Scenario:

Imagine you have an existing class, OldSystem, which is like a vintage computer that understands only outdated commands. You also have a modern class, NewSystem, which uses advanced commands that OldSystem can't comprehend. You need to find a way for the two systems to cooperate without changing their core code.

Problem:

You want to use the capabilities of the NewSystem, but it speaks a different "language" (has a different interface) than what OldSystem understands.

Solution - Adapter Pattern:

Enter the Adapter, your software translator. It sits between OldSystem and NewSystem, translating the modern commands from NewSystem into the language that OldSystem can understand.

Let's break it down:

  1. OldSystem: This represents an existing component, like a legacy system in software. It has methods, but they use outdated conventions that you can't change.

  2. NewSystem: This represents the new component, using advanced methods that OldSystem can't process.

  3. The Adapter: This is your bridge, implementing an interface that OldSystem can work with. It translates requests from NewSystem into something OldSystem can comprehend.

In code:

Step 1: The Existing OldSystem (Legacy System):

public class OldSystem {
    public void outdatedRequest() {
        System.out.println("Old System is processing outdated request.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: The Modern NewSystem:

public class NewSystem {
    public void advancedRequest() {
        System.out.println("New System is processing advanced request.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: The Adapter:

public class SystemAdapter extends OldSystem {
    private NewSystem newSystem;

    public SystemAdapter(NewSystem newSystem) {
        this.newSystem = newSystem;
    }

    public void processRequest() {
        // Translate the advanced request into something the OldSystem understands
        newSystem.advancedRequest();
    }
}
Enter fullscreen mode Exit fullscreen mode

Putting It All Together:

Now, you can use the SystemAdapter to access the advanced functionality of the NewSystem through OldSystem:

public class Main {
    public static void main(String[] args) {
        OldSystem oldSystem = new OldSystem();
        NewSystem newSystem = new NewSystem();
        SystemAdapter adapter = new SystemAdapter(newSystem);

        oldSystem.outdatedRequest(); // Use the old system
        adapter.processRequest();   // Use the new system via the adapter
    }
}
Enter fullscreen mode Exit fullscreen mode

By employing the Adapter pattern, you've connected the old and new systems seamlessly, allowing them to cooperate without any disruptions. The Adapter acts as a translation layer, enabling compatibility between the systems while maintaining their individuality, much like a language translator in our software development analogy.

😍 If you enjoy the content, please 👍 like, 🔄 share, and 👣 follow for more updates!
Join me on a professional journey through my LinkedIn profile: Linkedin Profile

Top comments (0)