diff --git a/adapter_demo/Advanced_Media_Player.java b/adapter_demo/Advanced_Media_Player.java new file mode 100644 index 000000000000..0adb80d79afa --- /dev/null +++ b/adapter_demo/Advanced_Media_Player.java @@ -0,0 +1,68 @@ +package com.example.adapterpattern; + +// Step 1: Target interface +interface MediaPlayer { + void play(String audioType, String fileName); +} + +// Step 2: Adaptee class +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); + } + } +} + +// Step 3: Advanced Media Player (Adaptee) +interface AdvancedMediaPlayer { + void playVlc(String fileName); + void playMp4(String fileName); +} + +class VlcPlayer implements AdvancedMediaPlayer { + public void playVlc(String fileName) { + System.out.println("Playing vlc file: " + fileName); + } + + public void playMp4(String fileName) { + // Do nothing + } +} + +class Mp4Player implements AdvancedMediaPlayer { + public void playVlc(String fileName) { + // Do nothing + } + + public void playMp4(String fileName) { + System.out.println("Playing mp4 file: " + fileName); + } +} + +// Step 4: Concrete class using adapter +class AudioPlayer implements MediaPlayer { + MediaAdapter mediaAdapter; + + @Override + public void play(String audioType, String fileName) { + if (audioType.equalsIgnoreCase("mp3")) { + System.out.println("Playing mp3 file: " + fileName); + } else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) { + mediaAdapter = new MediaAdapter(audioType); + mediaAdapter.play(audioType, fileName); + } else { + System.out.println("Invalid media type: " + audioType);