首页 适配器设计模式
文章
取消

适配器设计模式

适配器模式概念

将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。

应用场景

  1. 电源适配器将 220V 转化为 5V
  2. 在 LINUX 上运行 WINDOWS 程序
  3. 让播放器支持更多的媒体格式播放

    实现案例

    • 为媒体播放器和更高级的媒体播放器创建接口
      1
      2
      3
      
      public interface MediaPlayer {
      public void play(String audioType, String fileName);
      }
      
    • AdvancedMediaPlayer接口:
      1
      2
      3
      4
      
      public interface AdvancedMediaPlayer {
      public void playVlc(String fileName);
      public void playMp4(String fileName);
      }
      
    • 创建实现了 AdvancedMediaPlayer 接口的实现类。 ```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) { //什么也不做 } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- 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);
   }
}
  • 创建实现了 MediaPlayer 接口的适配器类。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    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 接口的实体类。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    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");
        }
     }
    }
    
  • 使用 AudioPlayer 来播放不同类型的音频格式。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    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");
     }
    }
    
本文由作者按照 CC BY 4.0 进行授权
载入天数...载入时分秒...