【设计模式】之适配器模式


一、什么是适配器

将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作。该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adaptee)和客户(Client)之间进行协调,从而达到兼容的目的。其核心是解决一致性的问题。

二、 适配器的实现实例

我们展示使用Adapter模式,通过下面的例子,在音频播放器只可以播放mp3文件,使用先进的音频播放器,可以播放VLC和mp4文件。

具体实现简介:

我们创建一个接口MediaPlayer接口和一个实现的MediaPlayer接口的具体类的AudioPlayer。AudioPlayer默认情况下,可以播放MP3格式的音频文件。

我们创建另一个接口AdvancedMediaPlayer和AdvancedMediaPlayer的具体实现类.这个具体实现类可以播放VLC和MP4格式的文件。

我们想的AudioPlayer播放其他格式。要达到这一点,我们已经创建了一个实现的MediaPlayer接口的适配器类MediaAdapter并使用AdvancedMediaPlayer对象,来播放所需的格式。

AudioPlayer使用适配器类MediaAdapter,,在不知道具体类的情况下,只需传递给它所需的音频类型,它就可以播放所需的格式。 AdapterPatternDemo这是我们的演示类,它将使用AudioPlayer类播放各种格式的。

类之间的继承关系:

【设计模式】之适配器模式_第1张图片

代码实现:

(1)创建两个接口 MediaPlayer和AdvancedMediaPlayer

   MediaPlayer.java接口

package study_02.designpattern.adapter;

public interface MediaPlayer {
	public void play(String audioType, String fileName);
}

AdvancedMediaPlayer.java接口

package study_02.designpattern.adapter;

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


 

(2)创建实现了AdvancedMediaPlayer接口的具体类

package study_02.designpattern.adapter;

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) {
      //do nothing
   }
}

 

package study_02.designpattern.adapter;

public class Mp4Player implements AdvancedMediaPlayer{
   @Override
   public void playVlc(String fileName) {
      //do nothing
   }
   @Override
   public void playMp4(String fileName) {
      System.out.println("Playing mp4 file. Name: "+ fileName);		
   }
}



 

(3)创建一个实现了MediaPlayer适配器类(Adapter)

package study_02.designpattern.adapter;

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


 

(4)创建一个具体对象类(AudioPlayer)实现了MediaPlayer(此类拥有适配器的引用)

package study_02.designpattern.adapter;

public class AudioPlayer implements MediaPlayer {
	   MediaAdapter mediaAdapter; 

	   @Override
	   public void play(String audioType, String fileName) {		

	      //inbuilt support to play mp3 music files
	      if(audioType.equalsIgnoreCase("mp3")){
	         System.out.println("Playing mp3 file. Name: "+ fileName);			
	      } 
	      //mediaAdapter is providing support to play other file formats
	      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");
	      }
	   }   
}


 

(5)用AudioPlayer类播放各种音乐格式

package study_02.designpattern.adapter;

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

(6)输出结果

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

 

三、参考资料

http://www.tutorialspoint.com/design_pattern/adapter_pattern.htm

你可能感兴趣的:(java,jvm,类,java设计模式,实例)