Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Adapter mode


May 27, 2021 Design mode


Table of contents


Adapter mode

Adapter Pattern is a bridge between two incompatible interfaces. This type of design pattern is structural and combines the functions of two separate interfaces.

This pattern involves a single class that is responsible for joining separate or incompatible interface functions. A s a real-world example, a card reader is used as an adapter between a memory card and a notebook. You insert the memory card into the card reader, and then the card reader into the notebook so that you can read the memory card from the notebook.

Let's demonstrate the use of adapter patterns with the following examples. Where the audio player device can only play mp3 files, vlc and mp4 files can be played by using a more advanced audio player.

Introduced

Intent: Transforms the interface of one class into another interface that the customer wants. Adapter mode allows classes that would not otherwise work together because of interface incompatibility to work together.

Main solution: The main solution in software systems, often to put some "existing objects" into the new environment, and the new environment requirements of the interface is not satisfied with the current object.

When to use: 1, the system needs to use existing classes, and such interfaces do not meet the needs of the system. 2 . Want to build a reusable class for working with some classes that are not too associated with each other, including some that may be introduced in the future, and these source classes do not necessarily have consistent interfaces. 3 , through interface conversion, insert one class into another class system. ( For example, tigers and birds, now have an additional flying tiger, without increasing the physical demand, add an adapter, in which to contain a tiger object, to achieve the flying interface.) )

How to resolve: Inheritance or dependency (recommended).

Key code: The adapter inherits or relies on existing objects to implement the desired target interface.

Application examples: 1, U.S. Electrical 110V, China 220V, there needs to be an adapter to convert 110V to 220V. 2 , JAVA JDK 1.1 provides the Enumeration interface, and in 1.2 provides the Iterator interface, and if you want to use the 1.2 JDK, you need the adapter mode to convert the Enumeration interface of the previous system into an Iterator interface. 3 . Run the WINDOWS program on LINUX. 4 , jdbc in JAVA.

Pros: 1, let any two unrelated classes run together. 2 , improve the re-use of the class. 3 , increase the transparency of the class. 4 , good flexibility.

Cons: 1, too much use of the adapter, will make the system very messy, not easy to grasp the whole. F or example, it is clear that the call is an A interface, in fact, the internal is adapted to the implementation of the B interface, a system if too many such a situation, it is tantum a disaster. S o if it's not necessary, you can refactor the system directly instead of using an adapter. 2 . Because JAVA inherits at most one class, it can only fit one adaptor class at most, and the target class must be abstract.

Scenario: When you are motivated to modify the interface of a functioning system, you should consider using adapter mode.

Note: The adapter was not added at the time of the detailed design, but rather resolved the issue of the project in service.

Realize

We have a MediaPlayer interface and an entity class AudioPlayer that implements the MediaPlayer interface. By default, AudioPlayer can play audio files in mp3 format.

We also have another interface, AdvancedMediaPlayer, and an entity class that implements the AdvancedMediaPlayer interface. This class can play files in vlc and mp4 formats.

We want AudioPlayer to play audio files in other formats. To do this, we need to create an adapter-like MediaAdapter that implements the MediaPlayer interface and play the desired format using the AdvancedMediaPlayer object.

AudioPlayer uses the adapter class MediaAdapter to deliver the required audio type without knowing the actual class that can play the desired format of audio. AdapterPatternDemo, our demo class uses the AudioPlayer class to play in a variety of formats.

Adapter mode

Step 1

Create interfaces for media players and more advanced media players.

MediaPlayer.java

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

AdvancedMediaPlayer.java

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

Step 2

Create an entity class that implements the AdvancedMediaPlayer interface.

VlcPlayer.java

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) {
      //什么也不做
   }
}

Mp4Player.java

public class Mp4Player implements AdvancedMediaPlayer{

   @Override
   public void playVlc(String fileName) {
      //什么也不做
   }

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

Step 3

Create an adapter class that implements the MediaPlayer interface.

MediaAdapter.java

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

   @Override
   public void play(String audioType, String fileName) {
      if(audioType.equalsIgnoreCase("vlc")){
         advancedMusicPlayer.playVlc(fileName);
      }else if(audioType.equalsIgnoreCase("mp4")){
         advancedMusicPlayer.playMp4(fileName);
      }
   }
}

Step 4

Create an entity class that implements the MediaPlayer interface.

AudioPlayer.java

public class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter; 

   @Override
   public void play(String audioType, String fileName) {        

      //播放 mp3 音乐文件的内置支持
      if(audioType.equalsIgnoreCase("mp3")){
         System.out.println("Playing mp3 file. Name: "+ fileName);          
      } 
      //mediaAdapter 提供了播放其他文件格式的支持
      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");
      }
   }   
}

Step 5

Use AudioPlayer to play different types of audio formats.

AdapterPatternDemo.java

public class AdapterPatternDemo {
   public static void main(String[] args) {
      AudioPlayer audioPlayer = new AudioPlayer();

      audioPlayer.play("mp3", "beyond the horizon.mp3");
      audioPlayer.play("mp4", "alone.mp4");
      audioPlayer.play("vlc", "far far away.vlc");
      audioPlayer.play("avi", "mind me.avi");
   }
}

Step 6

Verify the output.

Playing mp3 file. Name: beyond the horizon.mp3
Playing mp4 file. Name: alone.mp4
Playing vlc file. Name: far far away.vlc
Invalid media. avi format not supported