概述
1 适配器模式
2 代码
interface AdvancePlayer{
public void playMp4();
public void playVlc();
}
class VlcPlayer implements AdvancePlayer{
@Override
public void playMp4() {
}
@Override
public void playVlc() {
System.out.println("play vlc");
}
}
class Mp4Player implements AdvancePlayer{
@Override
public void playMp4() {
System.out.println("play mp4");
}
@Override
public void playVlc() {
}
}
interface Player{
public void play(String type);
}
class MediaAdapter implements Player{
AdvancePlayer advancePlayer;
@Override
public void play(String type) {
if (type.equals("mp4")){
advancePlayer = new Mp4Player();
advancePlayer.playMp4();
}else if (type.equals("vlc")){
advancePlayer = new VlcPlayer();
advancePlayer.playVlc();
}
}
}
class AudioPlayer implements Player{
private MediaAdapter mediaAdapter;
@Override
public void play(String type) {
mediaAdapter = new MediaAdapter();
mediaAdapter.play(type);
}
}
public class Test{
public static void main(String[] args){
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp4");
audioPlayer.play("vlc");
}
}
最后
以上就是欣喜高山为你收集整理的Structural model -- Structural model的全部内容,希望文章能够帮你解决Structural model -- Structural model所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复