/// <summary>
/// 创建接口
/// </summary>
public interface MediaPlayer
{
void Player(string audioType,string Filename);
}
public interface AdvancedMediaPlayer
{
void PlayerMP4(string Filename);
void PlayerVLC(string Filename);
}
/// <summary>
/// PlayerVLC接口的实体类
/// </summary>
public class VLCPlay :AdvancedMediaPlayer{
public void PlayerVLC(string Filename){
Report.Info("AdvancedMediaPlayer:PlayerVLC :"+Filename);
}
public void PlayerMP4(string Filename){
}
}
/// <summary>
/// PlayerVLC接口的实体类
/// </summary>
public class MP4Play :AdvancedMediaPlayer{
public void PlayerVLC(string Filename){
}
public void PlayerMP4(string Filename){
Report.Info("AdvancedMediaPlayer:PlayerMP4 :"+Filename);
}
}
/// <summary>
/// 适配器类
/// </summary>
public class MediaAdapter :MediaPlayer{
AdvancedMediaPlayer advancedMediaPlayer;
public MediaAdapter(string audioType){
if(audioType.Equals("MP4")){
advancedMediaPlayer=new MP4Play();
}else if(audioType.Equals("VLC")){
advancedMediaPlayer=new VLCPlay();
}
}
public void Player(string audioType,string Filename){
if(audioType.Equals("MP4")){
advancedMediaPlayer.PlayerMP4(Filename);
}else if(audioType.Equals("VLC")){
advancedMediaPlayer.PlayerVLC(Filename);
}
}
}
/// <summary>
/// 接口实体类
/// </summary>
public class AudioPlayer :MediaPlayer{
MediaAdapter mediaAdapter;
public void Player(string audioType,string Filename){
if(audioType.Equals("MP3")){
Report.Info("PlayerMP3 :"+Filename);
}else if(audioType.Equals("MP4")||audioType.Equals("VLC")){
mediaAdapter=new MediaAdapter(audioType);
mediaAdapter.Player(audioType,Filename);
}else{
Report.Info("输入无效的播放器,请重新输入");
}
}
}
Factory();
public void Factory(){
AudioPlayer audioPlayer =new AudioPlayer();
audioPlayer.Player("MP3","请先说你好");
audioPlayer.Player("MP4","麻雀");
audioPlayer.Player("VLC","星之画");
}
结果: