设计模式-适配器模式

适配器模式(Adapter),是作为两个不同接口的桥梁。

例子


源自https://www.runoob.com/design-pattern/adapter-pattern.html

MediaPlayer 接口的实现类AudioPlayer。默认情况下,AudioPlayer 可以播放 mp3 格式的音频文件。
AdvancedMediaPlayer 接口的实现类VlcPlayer 和 Mp4Player 可以播放 vlc 和 mp4 格式的文件。
我们的需求是:让AudioPlayer播放其他格式的音频文件。
为了实现这个需求,创建一个实现了MediaPlayer接口的适配器类 MediaAdapter,并使用 AdvancedMediaPlayer 对象来播放所需的格式。


设计模式-适配器模式_第1张图片

创建两个接口:播放器和高级播放器

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

实现高级播放器接口,用来播放Vlc和Mp4

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

适配器类,实现播放器接口

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接口的实体类

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

测试

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

输出:

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

灵魂

新建适配器类MediaAdapter,高级播放器接口作为成员变量,再让其作为AudioPlayer类的成员变量。

你可能感兴趣的:(java设计模式,设计模式)