This is interface player :
public interface MediaPlayer {
void play(String mediaType, String item);
}
This is basic player :
public class AudioPlayer implements MediaPlayer {
private MediaAdapter mediaAdapter ;
@Override
public void play(String mediaType, String item) {
if (mediaType.equals("mp3")) {
System.out.println("Playing mp3" + item);
} else if (mediaType.equals("vlc") || mediaType.equals("mp4")) {
mediaAdapter = new MediaAdapter(mediaType);
mediaAdapter.pluginPlayer(item);
} else {
System.out.println("invalid media type");
}
}
}
This is my adpater. It is not of type MediaPlayer.
public class MediaAdapter {
private AdvanceMediaPlayer advanceMediaPlayer;
public MediaAdapter(String mediaType) {
if (mediaType.equals("vlc")) {
advanceMediaPlayer = new VLCPlayer();
} else if (mediaType.equals("mp4")) {
advanceMediaPlayer = new MP4Player();
}
}
public void pluginPlayer(String item) {
advanceMediaPlayer.play(item);
}
}
This is advance media player.
public interface AdvanceMediaPlayer {
void play(String item);
}
This is a type of advance media player.
public class VLCPlayer implements AdvanceMediaPlayer {
@Override
public void play(String item) {
System.out.println("Playing vlc : " + item);
}
}
This is another advance media player.
public class MP4Player implements AdvanceMediaPlayer {
@Override
public void play(String item) {
System.out.println("Playing MP4 : " + item);
}
}
This is my test class.
public class Test {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "mp3 song");
audioPlayer.play("vlc", "vlc song");
audioPlayer.play("mp4", "mp4 song");
audioPlayer.play("avi", "avi songs");
}
}
Aucun commentaire:
Enregistrer un commentaire