Structural model -- Structural model
程序员文章站
2022-07-02 22:38:28
...
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");
}
}