[Pattern] Adapter pattern

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Pattern] Adapter pattern相关的知识,希望对你有一定的参考价值。

Check the adapter pattern 

https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm

 

Client: Call the common entry

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");
   

 

AudioPlayer: Common entry use adapter implement common interface

public class AudioPlayer implements MediaPlayer 
   MediaAdapter mediaAdapter; 

   @Override
   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");
      
      

 

Adapter: Adapter use implememtaion interface and implements common interface

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

 

MediaPlayer: The common interface

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

 

AdvancedMediaPlayer: The implementation interface

 

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

 

Implementation details:

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
   



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

 

Adapter Pattern

Introduction:you have a class with some functions which are not exactly what you can use directerly to sovle the problem you face with.so some adaption is needed to help you reuse the old class without any modification but you can also add some new functions to solve the fatal problem. In a word,we could build a new class with the old one.

example: 

You wanna to say "Hello world",before that you tend to do something else and clear the initialization after the class object died.

So what you have is:

class YouHave{
public void hello(){
System.out.print("Hello ");
}
public void world(){
System.out.println("world.");
}
}

Waht You wanna is:

Do something of initialization.
Hello world.
Do something to release the source.

 

We can use a interface to transit:

abstract class YouWanna{
abstract public void wanna();
}

Then build a new class(called adapter):

class Surrogate extends YouWanna{
private YouHave have;
public Surrogate(YouHave have){
this.have=have;
}
public void wanna(){
System.out.println("Do something of initialization.");
have.hello();
have.world();
System.out.println("Do something to release the source.");
}
}

Now you can use it:

public class AdapterPattern {
public static void main(String[] args) {
YouHave have=new YouHave();
Surrogate surrogate=new Surrogate(have);
surrogate.wanna();
}
}

The Adapter parttern is often confused with Proxy parttern ,but when we use an adapter,
we do not to keep the same interface as using the proxy one.
The end.

以上是关于[Pattern] Adapter pattern的主要内容,如果未能解决你的问题,请参考以下文章

[Design Pattern] Adapter Design Pattern

七适配器(Adapter)模式--结构模式(Structural Pattern)

java design pattern - adapter pattern

Adapter Pattern

Adapter Pattern

适配器模式(Adapter Pattern)