This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging

Adapter patterns serve as a bridge between incompatible interfaces. This type of design pattern is a structural pattern that combines the functionality of two separate interfaces.

This pattern involves a single class that is responsible for adding independent or incompatible interface functions. In a real world example, a card reader acts as an adapter between a memory card and a laptop. 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.

introduce

describe parsing
intentions Convert the interface of a class to another interface that the customer wants. The adapter pattern allows classes that otherwise would not work together due to interface incompatibilities to work together.
Mainly to solve The main solution is in the software system, often to put some “existing objects” into the new environment, and the new environment requirements of the interface is the current object can not meet.
When to use 1. The system needs to use an existing class, and the interface of this class does not meet the system’s needs. 2. Want to create a reusable class to work with classes that are not very related to each other, including some classes that may be introduced in the future, but which do not necessarily have a consistent interface. 3. Insert a class into another class system by interface transformation. (For example, tigers and Birds, there is now a flying tiger, without the need to add an entity, add an adapter that contains a tiger object to implement the flying interface.)
How to solve Inheritance or dependency (recommended).
The key code Adapters inherit or rely on existing objects to implement the desired target interface.
Examples of application 1, the United States electrical 110V, China 220V, there must be an adapter to convert 110V to 220V. JAVA JDK 1.1 provides the Enumeration interface, while 1.2 provides the Iterator interface. If you want to use JDK 1.2, you need to convert the Enumeration interface from the previous system to the Iterator interface. This is where the adapter pattern is needed. 3. Run WINDOWS programs on LINUX. 4. JDBC in JAVA.
advantages You can run any two classes that are not associated with each other. 2. Improved class reuse. 3. Increased class transparency. 4, good flexibility.
disadvantages 1, too much use of adapters, will make the system very messy, not easy to grasp the overall. For example, it is clear that the call is interface A, in fact, the internal interface is adapted to the implementation of B, A system if too much of this situation, is no different from A disaster. So if you don’t really need an adapter, you can simply refactor your system. 2. Since JAVA inherits at most one class, it can only fit one adaptor class at most, and the target class must be abstract.
Usage scenarios When you have an incentive to modify the interface of a functioning system, you should consider using the adapter pattern.
Matters needing attention The adapter was not added at detailed design time, but rather addressed the project in service.

implementation

Use the following example to demonstrate the use of the adapter pattern. The audio player device can only play MP3 files, and VLC and MP4 files can be played by using a more advanced audio player.

  1. There is aMediaPlayerInterface and an implementationMediaPlayerThe entity class of the interfaceAudioPlayer. By default,AudioPlayerYou can playmp3Format of audio files.
  2. There is another interfaceAdvancedMediaPlayerAnd implementedAdvancedMediaPlayerThe entity class of the interface. This class can playvlcmp4Format file.
  3. We want to makeAudioPlayerPlay audio files in other formats. To implement this, we need to create an implementationMediaPlayerAdapter class for the interfaceMediaAdapterAnd the use ofAdvancedMediaPlayerObject to play the desired format.
  4. AudioPlayerUsing the adapter classMediaAdapterPass the desired audio type, without knowing the actual class that can play audio in the desired format.AdapterPatternDemoClass USESAudioPlayerClass to play various formats.

\

Step 1- Create a play interface

Create interfaces for media players and more advanced media players.

// MediaPlayer.java
public interface MediaPlayer {
   public void play(String audioType, String fileName);
}
Copy the code
// AdvancedMediaPlayer.java
public interface AdvancedMediaPlayer { 
   public void playVlc(String fileName);
   public void playMp4(String fileName);
}
Copy the code

Step 2- Create an entity class

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) {// do nothing}}Copy the code
// 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); }}Copy the code

Step 3- Create an adaptation class

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); }}}Copy the code

Step 4- Create autoplay class

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) {/ / play mp3 music files built-in support the if (audioType. EqualsIgnoreCase (" mp3 ")) {System. Out. Println (" Playing mp3 file. Name:  "+ fileName); } / / mediaAdapter provides play other file format support 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"); }}}Copy the code

Step 5- Call the scenario class

// 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"); }}Copy the code

Step 6- Output results

Execute the program and print 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
Copy the code