Real Examples of Adapter Pattern in Object-Oriented Programming

The Adapter Pattern is a commonly used design pattern in object-oriented programming that allows incompatible interfaces to work together. In essence, it acts as a bridge between two incompatible interfaces, allowing them to interact seamlessly with each other. Here are some real-world examples of the Adapter Pattern in action:

Índice
  1. Example 1: Charging Your Phone
  2. Example 2: Using a Foreign Language Dictionary
  3. Example 3: Using Third-Party Libraries

Example 1: Charging Your Phone

When you charge your phone, you plug in a USB cable that connects your phone to a power source. The USB cable acts as an adapter, converting the electrical current from the power source into a format that your phone's battery can understand. Without this adapter, your phone would not be able to charge properly.

Example 2: Using a Foreign Language Dictionary

If you are trying to read a book in a foreign language, you may need to use a dictionary to look up unfamiliar words. However, if the dictionary is written in a language that you do not understand, it will be of no use to you. In this case, you can use an adapter in the form of a translation app or service to convert the words in the dictionary into a language that you can understand.

Example 3: Using Third-Party Libraries

When you are working with third-party libraries in your code, you may encounter interfaces that are incompatible with your own code. In this case, you can use an adapter to bridge the gap between the two interfaces and allow them to work together. This is often necessary when you are using legacy code or trying to integrate with systems that were not designed to work together.


public interface MediaPlayer {
   public void play(String audioType, String fileName);
}

public interface AdvancedMediaPlayer {    
   public void playVlc(String fileName);
   public void playMp4(String fileName);
}

public class VlcPlayer implements AdvancedMediaPlayer{
   public void playVlc(String fileName) {
      System.out.println("Playing vlc file. Name: "+ fileName);        
   }

   public void playMp4(String fileName) {
      //do nothing
   }
}

public class Mp4Player implements AdvancedMediaPlayer{

   public void playVlc(String fileName) {
      //do nothing
   }

   public void playMp4(String fileName) {
      System.out.println("Playing mp4 file. Name: "+ fileName);        
   }
}

public 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();
      }  
   }

   public void play(String audioType, String fileName) {

      if(audioType.equalsIgnoreCase("vlc")){
         advancedMusicPlayer.playVlc(fileName);
      }
      else if(audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer.playMp4(fileName);
      }
   }
}

public class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter; 

   public void play(String audioType, String fileName) {    

      //inbuilt support to play mp3 music files
      if(audioType.equalsIgnoreCase("mp3")){
         System.out.println("Playing mp3 file. Name: "+ fileName);            
      } 
      //mediaAdapter is providing support to play other file formats
      else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
         mediaAdapter = new MediaAdapter(audioType);
         mediaAdapter.play(audioType, fileName);
      }
      else{
         System.out.println("Invalid media. "+ audioType + " format not supported");
      }
   }
}

In conclusion, the Adapter Pattern is a powerful tool for making incompatible interfaces work together seamlessly. Whether you are charging your phone, using a foreign language dictionary, or integrating third-party libraries into your code, the Adapter Pattern can help you bridge the gap and get everything working smoothly.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information