From b1b430dd6048125a3df26ff789e42c3b6c3806fc Mon Sep 17 00:00:00 2001 From: Hammy Date: Mon, 14 Dec 2020 18:43:09 +0000 Subject: [PATCH] Implement Adapter Design Pattern --- .../media/player/AdapterPatternDemo.java | 14 +++++++++++ .../media/player/AdvancedMediaPlayer.java | 8 ++++++ .../src/adapter/media/player/AudioPlayer.java | 19 ++++++++++++++ .../src/adapter/media/player/MP4Player.java | 14 +++++++++++ .../adapter/media/player/MediaAdapter.java | 25 +++++++++++++++++++ .../src/adapter/media/player/MediaPlayer.java | 6 +++++ .../src/adapter/media/player/VLCPlayer.java | 14 +++++++++++ 7 files changed, 100 insertions(+) create mode 100644 Design Patterns/src/adapter/media/player/AdapterPatternDemo.java create mode 100644 Design Patterns/src/adapter/media/player/AdvancedMediaPlayer.java create mode 100644 Design Patterns/src/adapter/media/player/AudioPlayer.java create mode 100644 Design Patterns/src/adapter/media/player/MP4Player.java create mode 100644 Design Patterns/src/adapter/media/player/MediaAdapter.java create mode 100644 Design Patterns/src/adapter/media/player/MediaPlayer.java create mode 100644 Design Patterns/src/adapter/media/player/VLCPlayer.java diff --git a/Design Patterns/src/adapter/media/player/AdapterPatternDemo.java b/Design Patterns/src/adapter/media/player/AdapterPatternDemo.java new file mode 100644 index 0000000..fa5ff08 --- /dev/null +++ b/Design Patterns/src/adapter/media/player/AdapterPatternDemo.java @@ -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"); + + } +} diff --git a/Design Patterns/src/adapter/media/player/AdvancedMediaPlayer.java b/Design Patterns/src/adapter/media/player/AdvancedMediaPlayer.java new file mode 100644 index 0000000..c00d8e5 --- /dev/null +++ b/Design Patterns/src/adapter/media/player/AdvancedMediaPlayer.java @@ -0,0 +1,8 @@ +package adapter.media.player; + +public interface AdvancedMediaPlayer { + + void playVLC(String fileName); + + void playMP4(String fileName); +} diff --git a/Design Patterns/src/adapter/media/player/AudioPlayer.java b/Design Patterns/src/adapter/media/player/AudioPlayer.java new file mode 100644 index 0000000..1bb97c2 --- /dev/null +++ b/Design Patterns/src/adapter/media/player/AudioPlayer.java @@ -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); + } + } +} diff --git a/Design Patterns/src/adapter/media/player/MP4Player.java b/Design Patterns/src/adapter/media/player/MP4Player.java new file mode 100644 index 0000000..53b2825 --- /dev/null +++ b/Design Patterns/src/adapter/media/player/MP4Player.java @@ -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); + } +} diff --git a/Design Patterns/src/adapter/media/player/MediaAdapter.java b/Design Patterns/src/adapter/media/player/MediaAdapter.java new file mode 100644 index 0000000..6133d9f --- /dev/null +++ b/Design Patterns/src/adapter/media/player/MediaAdapter.java @@ -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); + } + } +} diff --git a/Design Patterns/src/adapter/media/player/MediaPlayer.java b/Design Patterns/src/adapter/media/player/MediaPlayer.java new file mode 100644 index 0000000..eefd7f1 --- /dev/null +++ b/Design Patterns/src/adapter/media/player/MediaPlayer.java @@ -0,0 +1,6 @@ +package adapter.media.player; + +public interface MediaPlayer { + + void play(String audioType, String fileName); +} diff --git a/Design Patterns/src/adapter/media/player/VLCPlayer.java b/Design Patterns/src/adapter/media/player/VLCPlayer.java new file mode 100644 index 0000000..974740e --- /dev/null +++ b/Design Patterns/src/adapter/media/player/VLCPlayer.java @@ -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 + } +}