The Configurator Pattern of Java Design Patterns

The Adapter Pattern acts as a bridge between two incompatible interfaces. This type of design pattern is a structural pattern that combines the functionality of two separate interfaces.

Intent: Convert the interface of a class to another interface that the client wants. The adapter pattern enables classes to work together that would otherwise not work together due to incompatible interfaces.

We demonstrate the use of the adapter pattern through the following example. Among them, the audio player device can only play mp3 files, by using a more advanced audio player to play vlc and mp4 files.

package cn.com.proxy;

public interface MediaPlayer {
	public void play(String audioType, String fileName);
}
package cn.com.proxy;
//Advanced player
public interface AdvancedMediaPlayer {
	public void playVlc(String fileName);
	public void playMp4(String fileName);
}

package cn.com.proxy;
//implement the advanced player
public class Mp4Player implements AdvancedMediaPlayer{

	@Override
	public void playVlc(String fileName) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void playMp4(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("The music you play is mp4: "+fileName);
	}

}
package cn.com.proxy;

public class VlcPlayer implements AdvancedMediaPlayer{
//implement the advanced player

	@Override
	public void playVlc(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("The music you are playing is Vlc: "+fileName);
	}

	@Override
	public void playMp4(String fileName) {
		// TODO Auto-generated method stub
		
	}

}
package cn.com.proxy;

public class MediaAdapter implements MediaPlayer{
	AdvancedMediaPlayer AdvancedMediaPlayer;//Define advanced player
	@Override//The file that calls the advanced player according to the playback file type
	public void play(String audioType, String fileName) {
		// TODO Auto-generated method stub
		if(audioType.equalsIgnoreCase("vlc")){
			AdvancedMediaPlayer.playVlc(fileName);
		}else if(audioType.equalsIgnoreCase("mp4")){
			AdvancedMediaPlayer.playMp4(fileName);
		}
	}//The initialization of the class instantiates different playback file types
		public MediaAdapter(String audioType){
		if(audioType.equalsIgnoreCase("vlc")){
			AdvancedMediaPlayer = new VlcPlayer();
		}else if(audioType.equalsIgnoreCase("mp4")){
			AdvancedMediaPlayer = new Mp4Player();
		}
		
	}

}
package cn.com.proxy;
import cn.com.proxy.MediaAdapter;
public class AudioPlay implements MediaPlayer{

	@Override
	public void play(String audioType, String fileName) {
		// TODO Auto-generated method stub
		if(audioType.equalsIgnoreCase("mp3")){
			System.out.println("You are playing MP3 " + fileName);
		}else if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){
			MediaAdapter MediaAdapter = new MediaAdapter (audioType);
			MediaAdapter.play(audioType, fileName);
		}else{
			System.out.println("No supported format...");
		}
	}

}
package cn.com.Test;

import cn.com.Master.Declaration;
import cn.com.Master.Master;
import cn.com.Master.intermiediaryAgent;
import cn.com.proxy.AudioPlay;

public class Test {
	public static void main(String[] args) {
		AudioPlay ap = new AudioPlay();
		ap.play("mp4", "----------------");
		ap.play("mp3", "----------------");
		ap.play("vlc", "----------------");
		ap.play("mrng", "----------------");
	}
}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325929147&siteId=291194637