Tuesday, March 7, 2023

Java adapter design pattern

Adapter design pattern

This pattern allows objects with incompatible interfaces to work together by wrapping one interface around the other.


The Adapter pattern is a design pattern in object-oriented programming that allows two incompatible interfaces to work together. In Java, the Adapter pattern is implemented using an Adapter class that acts as a bridge between two incompatible interfaces.

The Adapter class implements the interface that is expected by the client code, and it contains an instance of the class that implements the incompatible interface. The Adapter class then translates the methods of the client interface into the corresponding methods of the incompatible interface.

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


// Interface expected by the client interface MediaPlayer { public void play(String audioType, String fileName); } // Incompatible interface interface AdvancedMediaPlayer { public void playVlc(String fileName); public void playMp4(String fileName); } // Class that implements the incompatible interface class VlcPlayer implements AdvancedMediaPlayer { public void playVlc(String fileName) { System.out.println("Playing VLC file. Name: "+ fileName); } public void playMp4(String fileName) { // do nothing } } // Adapter class class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter(String audioType){ if(audioType.equalsIgnoreCase("vlc") ){ advancedMusicPlayer = new VlcPlayer(); } } public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase("vlc")){ advancedMusicPlayer.playVlc(fileName); } } } // Client code class AudioPlayer implements MediaPlayer { MediaAdapter mediaAdapter; public void play(String audioType, String fileName) { //play mp3 files if(audioType.equalsIgnoreCase("mp3")){ System.out.println("Playing MP3 file. Name: "+ fileName); } //mediaAdapter is providing support to play other file formats else if(audioType.equalsIgnoreCase("vlc")){ mediaAdapter = new MediaAdapter(audioType); mediaAdapter.play(audioType, fileName); } } } // Usage public class Main { public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond_the_horizon.mp3"); audioPlayer.play("vlc", "far_far_away.vlc"); } }

In this example, the MediaPlayer interface is the interface expected by the client code. The AdvancedMediaPlayer interface is the incompatible interface, and the VlcPlayer class implements this interface.

The MediaAdapter class is the adapter that translates the methods of the MediaPlayer interface into the corresponding methods of the AdvancedMediaPlayer interface. The AudioPlayer class is the client code that uses the MediaPlayer interface.

When the client code calls the play() method on the AudioPlayer object, the adapter is used to play the file in the desired format. If the file is in the MP3 format, it is played directly by the AudioPlayer object. If the file is in the VLC format, the adapter is used to play the file by calling the play() method on the MediaAdapter object, which in turn calls the playVlc() method on the VlcPlayer object.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home