Implement Adapter Design Pattern

master
Hammy 4 years ago
parent d23927fd6e
commit b1b430dd60

@ -0,0 +1,14 @@
package adapter.media.player;
public class AdapterPatternDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp4", "Never Say Never - Justin Bieber");
audioPlayer.play("vlc", "Better - TWICE");
audioPlayer.play("mp3", "Fake & True - TWICE");
audioPlayer.play("avi", "Sunflower - Post Malone");
}
}

@ -0,0 +1,8 @@
package adapter.media.player;
public interface AdvancedMediaPlayer {
void playVLC(String fileName);
void playMP4(String fileName);
}

@ -0,0 +1,19 @@
package adapter.media.player;
public class AudioPlayer implements MediaPlayer {
private MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing MP3 File | Name: " + fileName);
} else if (audioType.equalsIgnoreCase("mp4") || audioType.equalsIgnoreCase("vlc")) {
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else {
System.out.printf("Media Format '%s' Not Recognised!", audioType);
}
}
}

@ -0,0 +1,14 @@
package adapter.media.player;
public class MP4Player implements AdvancedMediaPlayer {
@Override
public void playVLC(String fileName) {
// Nothing Happens
}
@Override
public void playMP4(String fileName) {
System.out.println("Playing MP4 File | Name: " + fileName);
}
}

@ -0,0 +1,25 @@
package adapter.media.player;
public class MediaAdapter implements MediaPlayer {
private AdvancedMediaPlayer advancedMediaPlayer;
public MediaAdapter(String audioType) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMediaPlayer = new VLCPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMediaPlayer = new MP4Player();
}
}
@Override
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMediaPlayer.playVLC(fileName);
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMediaPlayer.playMP4(fileName);
}
}
}

@ -0,0 +1,6 @@
package adapter.media.player;
public interface MediaPlayer {
void play(String audioType, String fileName);
}

@ -0,0 +1,14 @@
package adapter.media.player;
public class VLCPlayer implements AdvancedMediaPlayer {
@Override
public void playVLC(String fileName) {
System.out.println("Playing VLC File | Name: " + fileName);
}
@Override
public void playMP4(String fileName) {
// Nothing happens
}
}
Loading…
Cancel
Save