Latest web development tutorials

Adapter Model

Adapter mode (Adapter Pattern) as a bridge between two incompatible interfaces. This type of design patterns belong structural model, which combines the features of two separate interfaces.

This model involves a single class that is responsible for independent or join incompatible interface functions. For real-life example, as a memory card reader is the adapter and laptop between. Your memory card into the reader, then the reader into the notebook, so that the memory card can be read through the notebook.

We demonstrate the use of the adapter mode by the following examples. Wherein the audio player device can only play mp3 files to play vlc and mp4 files by using a more advanced audio player.

Introduction

Intent: to convert the interface of a class into another interface clients expect.Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together.

Mainly to solve: the main solution in the software system, often to some of the "existing object" into a new environment, new environmental requirements and the interface is now the object can not be met.

When to use: 1, the system requires the use of an existing class, and such interfaces do not meet the needs of the system.2, want to create a class can be reused for, including some might work with some of the classes is not much related to each other in the future introduction of the class, these classes do not necessarily have the same source interface. 3, through the interface converter, a class into another class lines. (Such as tigers and birds, now more than a Tiger, without increasing the demand entities under an additional adapter, in which a tiger inclusive objects, implement fly interface.)

How to fix: Inherited or dependence (recommended).

The key code: adapter inheritance or dependent objects that already exist to achieve the desired target interface.

Application examples: 1, the United States Electric 110V, China 220V, there should be a 110V adapter into 220V.2, JAVA JDK 1.1 provides the Enumeration interface and provides the Iterator interface in 1.2, you want to use the 1.2 JDK, before the system will have to be converted to Enumeration interface Iterator interface, then you need an adapter pattern. 3, run the program on WINDOWS LINUX. 4, JAVA in jdbc.

Advantages: 1, can make any kind of run together two unrelated.2, improve the class reuse. 3, increase the transparency of the class. 4, good flexibility.

Disadvantages: 1, excessive use of the adapter, making the system very messy and difficult to grasp the whole.For example, the call is clearly seen A interface, in fact, be adapted to achieve B internal interface has become a system if this happens too much, is tantamount to a disaster. So if it is not necessary, you can not use an adapter, but directly to the system reconstructed. 2. As at most JAVA inherit a class, so at best adaptation adaptation by a class, and must target class is an abstract class.

Usage scenarios: There isa motivation to modify the interface normal operation of the system, then you should consider using adapter mode.

Note: The adapter is not added when the detailed design, but to solve the problem in service projects.

achieve

We have aMediaPlayerinterface, and a realization of the entity classAudioPlayerMediaPlayer interface. Bydefault,AudioPlayer can play mp3 audio file format.

We also have another interfaceAdvancedMediaPlayerand implemented entity classesAdvancedMediaPlayerinterface. This class can play the file vlc and mp4 formats.

We want to makeAudioPlayerplay audio files to other formats. To achieve this, we need to create an adapter class implementsMediaAdapterMediaPlayer interface and useAdvancedMediaPlayerobject to play the desired format.

AudioPlayerusing an adapter classMediaAdaptertransfer the desired audio type, do not need to know the format of the actual class can play audio needs.AdapterPatternDemo,our demonstration classes usingAudioPlayerclass to play a variety of formats.

Adapter pattern UML diagram

step 1

Players and more advanced media player to create an interface for the media.

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 entity classes realizedAdvancedMediaPlayerinterface.

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}
}

Mp4Player.java

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 aMediaPlayerclass implements the interface adapter.

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 entity classes realizedMediaPlayerinterface.

AudioPlayer.java

public class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter; 

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

      // Play mp3 music files, built-in support if (audioType.equalsIgnoreCase ( "mp3")) {
         System.out.println ( "Playing mp3 file Name:." + FileName);			
      } 
      // MediaAdapter provides play other file formats supported 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

AudioPlayer use 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 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