星星博客 »  > 

设计模式-适配器模式

4、适配器模式

  1. 结构型模式,作为两个不兼容的接口之间的桥梁

  2. 将一个类的接口转化为客户希望的另一个接口,使得原本由于接口不兼容而不能再一起工作的那些类可以一起工作。

  3. 解决将一些“现存的对象”放到新的环境中,而新环境要求的接口是现对象不能满足的。

  4. 缺点:过多的使用适配器,会让系统非常凌乱,明明看到调用的是A接口,其实内部被适配成了B接口的实现

    package com.yunyanchengyu.demo03;
    //mp3接口类
    public interface MediaPlayer {
        void play(String audioType,String fileName);
    }
    
    package com.yunyanchengyu.demo03;
    //vlc mp4
    public interface AdvanceMediaPlayer {
        void playVlc(String fileName);
        void playMp4(String fileName);
    }
    
    package com.yunyanchengyu.demo03;
    //AdvanceMediaPlayer的实现类
    public class Mp4Player implements AdvanceMediaPlayer {
    
    
        @Override
        public void playVlc(String fileName) {
    
        }
    
        @Override
        public void playMp4(String fileName) {
            System.out.println("play Mp4 file:" + fileName);
        }
    }
    
    package com.yunyanchengyu.demo03;
    //AdvanceMediaPlayer的实现类
    public class VlcPlayer implements AdvanceMediaPlayer {
        @Override
        public void playVlc(String fileName) {
            System.out.println("play vlc file:" + fileName);
        }
    
        @Override
        public void playMp4(String fileName) {
    
        }
    }
    
    package com.yunyanchengyu.demo03;
    //MediaPlayer的实现类,但是它作为了“适配器” 虽然实现的是MediaPlayer接口,但是其内部的实现都是AdvanceMediaPlayer接口的
    public class MediaAdapter implements MediaPlayer {
        private AdvanceMediaPlayer advanceMediaPlayer;
    
        public MediaAdapter(String audioType){
            if(audioType.equalsIgnoreCase("vlc")){
                advanceMediaPlayer = new VlcPlayer();
            }else{
                advanceMediaPlayer = new Mp4Player();
            }
        }
        @Override
        public void play(String audioType, String fileName) {
            if(audioType.equalsIgnoreCase("vlc")){
                advanceMediaPlayer.playVlc(fileName);
            }else{
                advanceMediaPlayer.playMp4(fileName);
            }
        }
    }
    
    package com.yunyanchengyu.demo03;
    
    //MediaPlayer功能对接适配器之后的扩充类
    public class AudioPlayer implements MediaPlayer {
        private MediaPlayer mediaPlayer;
        @Override
        public void play(String audioType, String fileName) {
            if(audioType.equalsIgnoreCase("mp3")){
                System.out.println("Playing map3 file:" + fileName);
            }else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
               mediaPlayer = new MediaAdapter(audioType);
               mediaPlayer.play(audioType,fileName);
            }else{
                System.out.println("格式不支持");
            }
        }
    //程序入口
        public static void main(String[] args) {
            MediaPlayer 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");
        }
    }
    

相关文章