#
Adapter (or Wrapper)
This tutorial explains to you the design pattern named Adapter (or Wrapper) (which is a Structural Pattern).
#
Adapter (or Wrapper) Pattern - theory
An Adapter class makes classes with incompatible interfaces work together.
Take a look at the following UML diagram:
MyPlayer is a class which initially have used "Mp3Player" & "WmaPlayer" class in order to play sound files. A new requirement states that "MyPlayer" class should also play "mp4" and "avi" files. This was/is possible using the "MediaPlayer" (interface), "Mp4Player" and "AviPlayer" classes. The main question is how we can make the old "MyPlayer" class to play "mp4" and "avi" files.
All we have to do is to add a new Java adapter (the "PlayerAdapter" class) and you modify the "MyPlayer" class. The result is that in "AdapterPatternExample" class we have only to add the code we need for playing audio files.
#
Adapter (or Wrapper) Pattern - example
Here is that example using the Adapter (or Wrapper) Design Pattern in Java:
package adapter.example.java;
public interface Player {
public void play1(String fileOnDisk);
public void play2(String fileType, String fileOnDisk);
}
package adapter.example.java;
public class Mp3Player implements Player{
public void play1(String fileOnDisk){
System.out.println("You are playing "+fileOnDisk+". It is a MP3 file !");
};
public void play2(String fileType, String fileOnDisk){};
}
package adapter.example.java;
public class WmaPlayer implements Player{
public void play1(String fileOnDisk){
System.out.println("You are playing "+fileOnDisk+". It is a WMA file !");
};
public void play2(String fileType, String fileOnDisk){};
}
package adapter.example.java;
public class MyPlayer {
Player player;
public void listen(String fileType, String fileOnDisk) {
if(fileType.equalsIgnoreCase("mp3")){
player = new Mp3Player();
player.play1(fileOnDisk);
}
else if(fileType.equalsIgnoreCase("wma")){
player = new WmaPlayer();
player.play1(fileOnDisk);
}
else if(fileType.equalsIgnoreCase("mp4") ||
fileType.equalsIgnoreCase("avi") ) {
player = new PlayerAdapter();
player.play2(fileType, fileOnDisk);
}
}
}
package adapter.example.java;
public class PlayerAdapter implements Player {
MediaPlayer mediaPlayer;
public void play2(String audioType, String audioFile) {
if(audioType.equalsIgnoreCase("mp4")){
mediaPlayer = new Mp4Player();
mediaPlayer.playMp4(audioFile);
}
else if(audioType.equalsIgnoreCase("avi")){
mediaPlayer = new AviPlayer();
mediaPlayer.playAvi(audioFile);
}
}
@Override
public void play1(String fileOnDisk) {}
}
package adapter.example.java;
public interface MediaPlayer {
void playMp4(String audioFile);
void playAvi(String audioFile);
}
package adapter.example.java;
public class Mp4Player implements MediaPlayer{
@Override
public void playMp4(String audioFile){
System.out.println("Mp4Player play a MP4 file.");
};
@Override
public void playAvi(String audioFile){
};
}
package adapter.example.java;
public class AviPlayer implements MediaPlayer{
@Override
public void playMp4(String audioFile){
};
@Override
public void playAvi(String audioFile){
System.out.println("AviPlayer play a AVI file.");
};
}
package adapter.example.java;
public class MainJavaAdaptorExample {
public static void main(String[] args) {
MyPlayer player = new MyPlayer();
player.listen("mp3", "Melody #1.mp3");
player.listen("wma", "Melody #2.wma");
player.listen("mp4", "My favorite movie.mp4");
player.listen("avi", "My favorite comedy.avi");
}
}
When you run this example you will receive the following result: