Adapter Design Pattern

The Adapter design pattern is a structural pattern that enables two incompatible interfaces to work together. In other words, it acts as a bridge between two interfaces that are not compatible with each other, by creating an adapter that allows the interfaces to work together. In this article, we will discuss the Adapter pattern in Java, including its implementation and practical use cases.

Implementation of Adapter Pattern in Java

The Adapter pattern consists of three main components: the Target interface, the Adaptee interface, and the Adapter class.

Target interface: This is the interface that defines the operations or methods that the client wants to use.

Adaptee interface: This is the interface that defines the operations or methods that the client wants to use, but which are not compatible with the Target interface.

Adapter class: This is the class that implements the Target interface and uses an instance of the Adaptee interface to provide the functionality required by the client.

Here’s an example implementation of the Adapter pattern in Java:

Define the Target interface:

public interface MediaPlayer {
public void play(String mediaType, String fileName);
}

Define the Adaptee interface:

public interface AdvancedMediaPlayer {
public void playVlc(String fileName);
public void playMp4(String fileName);
}


Create the Adapter class that implements the Target interface and uses an instance of the Adaptee interface to provide the required functionality:

public class MediaPlayerAdapter implements MediaPlayer {
private AdvancedMediaPlayer advancedMediaPlayer;
public MediaPlayerAdapter(String mediaType) {
    if(mediaType.equalsIgnoreCase("vlc")) {
        advancedMediaPlayer = new VlcPlayer();
    } else if (mediaType.equalsIgnoreCase("mp4")) {
        advancedMediaPlayer = new Mp4Player();
    }
}

public void play(String mediaType, String fileName) {
    if(mediaType.equalsIgnoreCase("vlc")) {
        advancedMediaPlayer.playVlc(fileName);
    } else if (mediaType.equalsIgnoreCase("mp4")) {
        advancedMediaPlayer.playMp4(fileName);
    }
}
}


Use the Adapter class to play media files:

public static void main(String[] args) {
MediaPlayer mediaPlayer = new MediaPlayerAdapter("mp4");
mediaPlayer.play("mp4", "movie.mp4");
mediaPlayer = new MediaPlayerAdapter("vlc");
mediaPlayer.play("vlc", "movie.vlc");
}


In this example, we define the Target interface MediaPlayer that specifies a method for playing media files. We also define the Adaptee interface AdvancedMediaPlayer that specifies methods for playing VLC and MP4 files. We then create the Adapter class MediaPlayerAdapter that implements the Target interface and uses an instance of the Adaptee interface to provide the required functionality. Finally, we use the Adapter class to play media files.

Practical Use Cases of Adapter Pattern in Java

The Adapter pattern is commonly used in Java applications to:

Integrate third-party libraries: Sometimes, we may need to use third-party libraries in our application that have interfaces that are not compatible with our existing code. The Adapter pattern can be used to adapt the third-party library’s interface to our application’s interface.

Legacy code integration: When we want to reuse old code with new systems, we may face the problem of incompatible interfaces. The Adapter pattern can be used to adapt the old code’s interface to the new system’s interface.

Multiple interface integration: In cases where a class needs to implement multiple interfaces, but cannot do so directly because the interfaces are incompatible, the Adapter pattern can be used to provide a common interface.

Conclusion

The Adapter pattern is a useful pattern that allows two incompatible interfaces to work together. It provides a bridge between the interfaces, allowing them to communicate with each other.