Java Design patterns
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class AdapterPatternDemo {
|
||||
public static void main(String[] args) {
|
||||
AudioPlayer audioPlayer = new AudioPlayer();
|
||||
audioPlayer.playMusic("mp3", "song1.mp3");
|
||||
audioPlayer.playMusic("mp4", "song2.mp4");
|
||||
audioPlayer.playMusic("vlc", "song3.vlc");
|
||||
audioPlayer.playMusic("xyz", "song4.avi");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public interface AdvancedMediaPlayer {
|
||||
void playVlcPlayer(String fileName);
|
||||
void playMp4Player(String fileName);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class AudioPlayer implements MediaPlayer {
|
||||
private MediaAdapter mediaAdapter;
|
||||
@Override
|
||||
public void playMusic(String audioType, String fileName) {
|
||||
//the mp3 format is supported by AudioPlayer itself and it doesn't need adapter here.
|
||||
if(audioType.equalsIgnoreCase("mp3")){
|
||||
System.out.println("Playing mp3 file: " + fileName);
|
||||
}
|
||||
//to support other formats, we will need the MediaAdapter
|
||||
else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
|
||||
mediaAdapter = new MediaAdapter(audioType);
|
||||
mediaAdapter.playMusic(audioType, fileName);
|
||||
}else{
|
||||
System.out.println("The given format: " + audioType + " is not supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class MediaAdapter implements MediaPlayer {
|
||||
|
||||
public static final String VLC = "vlc";
|
||||
public static final String MP_4 = "mp4";
|
||||
|
||||
private AdvancedMediaPlayer advancedMusicPlayer;
|
||||
public MediaAdapter(String audioType){
|
||||
if(audioType.equalsIgnoreCase(VLC) ){
|
||||
advancedMusicPlayer = new VlcMusicPlayer();
|
||||
}else if (audioType.equalsIgnoreCase(MP_4)){
|
||||
advancedMusicPlayer = new Mp4MusicPlayer();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playMusic(String audioType, String fileName) {
|
||||
if(audioType.equalsIgnoreCase(VLC)){
|
||||
advancedMusicPlayer.playVlcPlayer(fileName);
|
||||
}else if(audioType.equalsIgnoreCase(MP_4)){
|
||||
advancedMusicPlayer.playMp4Player(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public interface MediaPlayer{
|
||||
void playMusic(String audioType, String fileName);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class Mp4MusicPlayer implements AdvancedMediaPlayer{
|
||||
|
||||
@Override
|
||||
public void playVlcPlayer(String fileName) {
|
||||
//do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playMp4Player(String fileName) {
|
||||
System.out.println("Playing mp4 file: "+ fileName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package javadevjournal.design.structural.adapter;
|
||||
|
||||
/**
|
||||
* @author Kunwar
|
||||
*/
|
||||
public class VlcMusicPlayer implements AdvancedMediaPlayer{
|
||||
@Override
|
||||
public void playVlcPlayer(String fileName){
|
||||
System.out.println("Playing vlc file: "+ fileName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void playMp4Player(String fileName) {
|
||||
//do nothing
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user