博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java适配器模式
阅读量:5295 次
发布时间:2019-06-14

本文共 3625 字,大约阅读时间需要 12 分钟。

默认的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.mp3Playing mp4 file. Name: alone.mp4Playing vlc file. Name: far far away.vlcInvalid media. avi format not supported

 

转载于:https://www.cnblogs.com/Java-Starter/p/9688911.html

你可能感兴趣的文章
开源网络漏洞扫描软件
查看>>
yum 命令跳过特定(指定)软件包升级方法
查看>>
创新课程管理系统数据库设计心得
查看>>
Hallo wolrd!
查看>>
16下学期进度条2
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
Chapter 3 Phenomenon——12
查看>>
中小学教育缴费遇到的一些问题
查看>>
C语言中求最大最小值的库函数
查看>>
js学习(精华帖)
查看>>
和小哥哥一起刷洛谷(1)
查看>>
jquery对id中含有特殊字符的转义处理
查看>>
获取元素样式信息于三中获取方式的区别
查看>>
遇麻烦,Win7+Ubuntu12.10+Archlinux12.10 +grub
查看>>
SqlBulkCopy大批量导入数据
查看>>
chrome(谷歌浏览器)“无法从该网站添加应用、扩展程序和用户脚本”问题
查看>>
HTTP协议 (四) 缓存
查看>>
python学习之random
查看>>
使用onclick跳转到其他页面/跳转到指定url
查看>>
【转载】测试计划模板
查看>>