Skip to content

Commit 8719bf9

Browse files
Added Adapter Pattern
1 parent 8a231cc commit 8719bf9

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Reference: https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
3+
*/
4+
5+
interface MediaPlayer {
6+
public void play(String audioType, String fileName);
7+
}
8+
9+
interface AdvancedMediaPlayer {
10+
public void playVlc(String fileName);
11+
public void playMp4(String fileName);
12+
}
13+
14+
class VlcPlayer implements AdvancedMediaPlayer{
15+
@Override
16+
public void playVlc(String fileName) {
17+
System.out.println("Playing vlc file. Name: "+ fileName);
18+
}
19+
20+
@Override
21+
public void playMp4(String fileName) {
22+
// do nothing
23+
}
24+
}
25+
26+
class Mp4Player implements AdvancedMediaPlayer{
27+
28+
@Override
29+
public void playVlc(String fileName) {
30+
// do nothing
31+
}
32+
33+
@Override
34+
public void playMp4(String fileName) {
35+
System.out.println("Playing mp4 file. Name: "+ fileName);
36+
}
37+
}
38+
39+
class MediaAdapter implements MediaPlayer {
40+
41+
AdvancedMediaPlayer advancedMusicPlayer;
42+
43+
public MediaAdapter(String audioType){
44+
45+
if(audioType.equalsIgnoreCase("vlc") ){
46+
advancedMusicPlayer = new VlcPlayer();
47+
48+
}else if (audioType.equalsIgnoreCase("mp4")){
49+
advancedMusicPlayer = new Mp4Player();
50+
}
51+
}
52+
53+
@Override
54+
public void play(String audioType, String fileName) {
55+
56+
if(audioType.equalsIgnoreCase("vlc")){
57+
advancedMusicPlayer.playVlc(fileName);
58+
}
59+
else if(audioType.equalsIgnoreCase("mp4")){
60+
advancedMusicPlayer.playMp4(fileName);
61+
}
62+
}
63+
}
64+
65+
class AudioPlayer implements MediaPlayer {
66+
MediaAdapter mediaAdapter;
67+
68+
@Override
69+
public void play(String audioType, String fileName) {
70+
71+
// inbuilt support to play mp3 music files
72+
if(audioType.equalsIgnoreCase("mp3")){
73+
System.out.println("Playing mp3 file. Name: " + fileName);
74+
}
75+
76+
// mediaAdapter is providing support to play other file formats
77+
else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
78+
mediaAdapter = new MediaAdapter(audioType);
79+
mediaAdapter.play(audioType, fileName);
80+
}
81+
82+
else{
83+
System.out.println("Invalid media. " + audioType + " format not supported");
84+
}
85+
}
86+
}
87+
88+
public class AdapterPattern {
89+
public static void main(String[] args) {
90+
AudioPlayer audioPlayer = new AudioPlayer();
91+
92+
audioPlayer.play("mp3", "beyond the horizon.mp3");
93+
audioPlayer.play("mp4", "alone.mp4");
94+
audioPlayer.play("vlc", "far far away.vlc");
95+
audioPlayer.play("avi", "mind me.avi");
96+
}
97+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
### Adapter Pattern
2+
3+
### Introduction
4+
5+
Adapter pattern works as a bridge between two incompatible interfaces. This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
6+
7+
### Advantages
8+
9+
- The code is reusable and flexible.
10+
- Clean code — because the client/context does not use a different interface in each concrete class and can use polymorphism to swap between different adapters.
11+
12+
### Disadvantages
13+
14+
- Cannot override methods in Adaptee.
15+
- Cannot reuse Adapter with subclasses of Target.
16+
- Adapter and Adaptee are different objects (Need to maintain relation between Adaptee and his Adapter).
17+
18+
### Programming
19+
20+
- Here, we have a MediaPlayer interface and a concrete class AudioPlayer implementing the MediaPlayer interface. AudioPlayer can play mp3 format audio files by default.
21+
22+
- We are having another interface AdvancedMediaPlayer and concrete classes implementing the AdvancedMediaPlayer interface. These classes can play vlc and mp4 format files.
23+
24+
- We want to make AudioPlayer to play other formats as well. To attain this, we have created an adapter class MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayer objects to play the required format.
25+
26+
- AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing the actual class which can play the desired format. AdapterPattern, our demo class will use AudioPlayer class to play various formats.

0 commit comments

Comments
 (0)