DEV Community

Harsh Mange
Harsh Mange

Posted on • Originally published at harshmange.hashnode.dev on

What is the Adapter Design Pattern?

Adapter Design Pattern is a structural design pattern that is used to bridge the gap between two incompatible interfaces. This pattern enables objects with incompatible interfaces to work together. The adapter pattern is also known as the wrapper pattern because it wraps an existing class with a new interface to make it compatible with other classes.

In software development, we often come across scenarios where we need to integrate new functionality with existing code. However, the new functionality may have a different interface than the existing code. This is where the adapter pattern comes into play.

Components

  1. Target: This is the interface that is expected by the client code. The client code communicates with the target interface to access the functionality of the adapter.

  2. Adapter: This is the class that implements the target interface and wraps an existing class. The adapter class translates the requests from the target interface to the existing class.

  3. Adaptee: This is the existing class that needs to be adapted to work with the target interface.

  4. Client: This is the code that uses the adapter to access the functionality of the adaptee.

Example

Suppose we have a MusicPlayer class that plays music files. The MusicPlayer class has a playMusic() method that takes a filename as an argument and plays the corresponding music file. Now, we want to add support for playing video files. However, the MusicPlayer class does not have a method to play video files. We can use the adapter pattern to bridge this gap.

We will create a VideoPlayer class that plays video files. The VideoPlayer class has a playVideo() method that takes a filename as an argument and plays the corresponding video file. We will create an adapter class, MediaAdapter, that implements the MusicPlayer interface and wraps the VideoPlayer class. The adapter class will translate the playMusic() method to the playVideo() method.

UML diagram

+--------------+ +-------------+ +--------------+
| Target | | Adapter | | Adaptee |
+--------------+ +-------------+ +--------------+
| + request() | | + request() | | + specificReq() |
+--------------+ +-------------+ +--------------+
        ^ ^ ^
        | | |
        +-------------------------|------------------------+
                                  |
                           +--------------+
                           | Client |
                           +--------------+
                           | + main() |
                           +--------------+

Enter fullscreen mode Exit fullscreen mode

The MusicPlayer interface is the target interface. The MediaAdapter class is the adapter that implements the MusicPlayer interface and wraps the VideoPlayer class. The VideoPlayer class is the adaptee that needs to be adapted to work with the MusicPlayer interface.

Implementation

// Target interface
public interface MusicPlayer {
    public void playMusic(String filename);
}

// Adaptee class
public class VideoPlayer {
    public void playVideo(String filename) {
        System.out.println("Playing video: " + filename);
    }
}

// Adapter class
public class MediaAdapter implements MusicPlayer {
    private VideoPlayer videoPlayer;

    public MediaAdapter(VideoPlayer videoPlayer) {
        this.videoPlayer = videoPlayer;
    }

    public void playMusic(String filename) {
        // Call the playVideo method of the adaptee class
        videoPlayer.playVideo(filename);
    }
}

// Client code
public class Client {
    public static void main(String[] args) {
        MusicPlayer musicPlayer = new MediaAdapter(new VideoPlayer());
        musicPlayer.playMusic("song.mp3"); // Calls the playVideo method of VideoPlayer class
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, we have created an adapter class, MediaAdapter, that implements the MusicPlayer interface and wraps the VideoPlayer class. The playMusic() method of the MediaAdapter class calls the playVideo() method of the VideoPlayer class to play the video file.

The client code creates an instance of the MediaAdapter class and calls its playMusic() method to play the music file. The adapter translates the playMusic() method to the playVideo() method of the adaptee class.

Top comments (0)