Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created March 11, 2021 17:36
Show Gist options
  • Save viveknaskar/3c9673e35a96a4e44c549fb9a3930629 to your computer and use it in GitHub Desktop.
Save viveknaskar/3c9673e35a96a4e44c549fb9a3930629 to your computer and use it in GitHub Desktop.
An illustration of Adapter Design Pattern
/**
* An illustration of adapter design pattern
*/
public class AdapterExample {
public static void main(String[] args) {
/**
* Using AudioPlayer for playing different audio formats
*/
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "Waiting For Love.mp3");
audioPlayer.play("vlc", "Wake Me Up.vlc");
audioPlayer.play("mp4", "Summer of 69.mp4");
audioPlayer.play("wma", "Lady.wma");
}
}
interface MediaPlayer {
void play(String audioType, String fileName);
}
interface AdvancedMediaPlayer {
void playVlc(String fileName);
void playMp4(String fileName);
}
class VlcPlayer implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {
System.out.println("Playing vlc file. Name: "+ fileName);
}
@Override
public void playMp4(String fileName) {
//do nothing
}
}
class Mp4Player implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {
//do nothing
}
@Override
public void playMp4(String fileName) {
System.out.println("Playing mp4 file. Name: "+ fileName);
}
}
/**
* The Adapter will be named MediaAdapter and it must implement
* the MediaPlayer interface. The FormatAdapter class must have a
* reference to AdvancedMediaPlayer, the incompatible interface.
*/
class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType){
if(audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer = new VlcPlayer();
} else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer.playVlc(fileName);
}
else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
/**
* inbuilt support to play mp3 music 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")
|| audioType.equalsIgnoreCase("mp4")){
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
}
else{
System.out.println("Invalid media. " + audioType + " format not supported");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment