Java single application - Architecture Mode - 03. Design mode - 06. Adapter mode

Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-adapter-pattern.html
More tutorials: Beam cloud - free course

Adapter mode

Serial number Chapter in text video
1 Summary -
2 Realization -

Please refer to the navigation above for reading

1. overview

The Adapter Pattern serves as a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.

This pattern involves a single class that is responsible for adding independent or incompatible interface functions. For example, the card reader is used as the adapter between the memory card and the notebook. You insert the memory card into the card reader, and then insert the card reader into the notebook, so that the memory card can be read from the notebook.

We use the following example to demonstrate the use of adapter pattern. Among them, the audio player device can only play mp3 files, by using a more advanced audio player to play vlc and mp4 files.

Intention:

Convert the interface of one class to another that the customer wants. The adapter pattern allows classes that could not work together because the interfaces are incompatible to work together.

Main solutions:

It is mainly solved in the software system that some "existing objects" are often put into the new environment, and the interface required by the new environment is not satisfied by the existing objects.

When to use:

  • The system needs to use existing classes, and the interface of this class does not meet the needs of the system.
  • You want to build a reusable class for working with some classes that are not very related to each other, including some classes that may be introduced in the future. These source classes may not have consistent interfaces.
  • Through interface transformation, one class is inserted into another class system. (for example, there is an additional flying tiger for tigers and birds. Without increasing the demand of entities, an adapter is added to contain a tiger object to realize the flying interface.)

How to solve:

Inheritance or dependency (recommended).

Key code:

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

Application example:

  • American electric 110V, China 220V, there must be an adapter to convert 110V to 220V.
  • Java JDK 1.1 provides the Enumeration interface, while in 1.2 it provides the Iterator interface. If you want to use the 1.2 JDK, you need to convert the Enumeration interface of the previous system to the Iterator interface, and then you need the adapter mode.
  • Run the WINDOWS program on LINUX.
  • jdbc in Java.

Advantage:

  • You can have any two classes that are not associated run together.
  • Improve the reuse of classes.
  • Increased class transparency.
  • Good flexibility.

Disadvantages:

  • Excessive use of adapters will make the system very messy and difficult to grasp as A whole. For example, it is obvious that the A interface is called, but in fact, it is internally adapted to the B interface implementation. If there are too many such cases in A system, it is no different from A disaster. So if it's not necessary, you can refactor the system directly instead of using the adapter.
  • Since Java inherits at most one class, at most one adapter class can be adapted, and the target class must be an abstract class.

Usage scenario:

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

matters needing attention:

The adapter was not added at the time of detailed design, but solved the problem of the project in service.

2. implementation

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

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

We want AudioPlayer to play audio files in other formats. To achieve this function, we need to create an adapter class MediaAdapter that implements the MediaPlayer interface, and use the AdvancedMediaPlayer object to play the required format.

AudioPlayer uses the adapter class MediaAdapter to deliver the required audio type. You do not need to know the actual class that can play the required format audio. AdapterPatternDemo, our demo class uses the AudioPlayer class to play various formats.

Step 1

Create interfaces for media players and more advanced media players.

MediaPlayer.java, the code is as follows:

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

AdvancedMediaPlayer.java, the code is as follows:

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

Step 2

Create an entity class that implements the AdvancedMediaPlayer interface.

VlcPlayer.java, the code is as follows:

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) {
      //Do nothing
   }
}

Mp4Player.java, the code is as follows:

public class Mp4Player implements AdvancedMediaPlayer{

   @Override
   public void playVlc(String fileName) {
      //Do nothing
   }

   @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, the code is as follows:

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, the code is as follows:

public class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter; 

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

      //Built in support for playing mp3 music files
      if(audioType.equalsIgnoreCase("mp3")){
         System.out.println("Playing mp3 file. Name: "+ fileName);         
      } 
      //mediaAdapter provides support for playing 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");
      }
   }   
}

Step 5

Use AudioPlayer to play different types of audio formats.

AdapterPatternDemo.java, the code is as follows:

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

Execute the program and output the result:

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

Last article: Archetypal model
Next article: Bridging mode

If you are interested in the content of the course, you can scan the code to pay attention to our official account or QQ group, and pay attention to our curriculum updates in time.


Tags: Java MediaPlayer JDK Windows

Posted on Thu, 12 Mar 2020 02:46:46 -0400 by GetPutDelete