Java适配器模式

Posted 多读书,少说话

tags:

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

默认的MediaPlayer接口只能播放mp3格式的音乐,然而我们现在想播放vlc和mp4格式的音乐,

怎么做呢?

这里我们引入AdvancedMediaPlayer接口,AdvancedMediaPlayer接口可以播放vlc和mp4格式的音乐。

怎么将AdvancedMediaPlayer接口“适配”到MediaPlayer接口中呢?

这里引入MediaAdapter类,这个类用来适配vlc和mp4格式的音乐

最后用AudioPlayer类实现MediaPlayer接口,适配的工作交给MediaAdapter类

 类结构图如下:

 

VlcPlayer.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:58
 * @Description:
 */


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

 

Mp4Player.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:58
 * @Description:
 */

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.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:57
 * @Description:
 */
public interface MediaPlayer {
    public void play(String audioType, String fileName);
}

 

MediaAdapter.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:58
 * @Description:
 */


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

 

AudioPlayer.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:59
 * @Description:
 */


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

 

AdvancedMediaPlayer.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:57
 * @Description:
 */

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

 

AdapterPatternDemo.java

package com.advance.adapter;

/**
 * @Auther: 谷天乐
 * @Date: 2018/9/20 22:59
 * @Description:
 */

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

 

以上是关于Java适配器模式的主要内容,如果未能解决你的问题,请参考以下文章

需要从寻呼机适配器识别当前视图片段

Java设计模式之适配器模式

Java设计模式应用——适配器模式

大话设计模式-适配器模式(大鸟和小菜Java版)

在活动中访问片段?

Java读源代码学设计模式:适配器Adapter