PHP适配器模式

2022-10-11 21:02:42 199 0
魁首哥

//适配器模式

class Mp4

{

public function play($fileName)

{

echo ‘play ‘.$fileName.'
’;

}

}

class mp3

{

public function play($fileName)

{

echo ‘play ‘.$fileName.'
’;

}

}

class MediaAdapter

{

public function play($audioType, $fileName)

{

$funcName=’get_’.strtolower($audioType);

if (method_exists($this, $funcName))

{

$player = call_user_func(array($this,$funcName));

$player->play($fileName);

}else{

echo ‘Invalid media.’.$audioType.” format not supported”;

}

}

private function get_ mp4 ()

{

return new Mp4 ();

}

private function get_ Mp3 ()

{

return new Mp3();

}

}

class vlc

{

public function play($fileName)

{

echo ‘play ‘.$fileName.'
’;

}

}

class AudioPlayer extends MediaAdapter

{

protected function get_vlc()

{

return new Vlc();

}

}

$audioPlayer = new AudioPlayer();

$audioPlayer->play(‘Mp3′,’horizon.mp3’);

$audioPlayer->play(‘Mp4′,’alone.mp4’);

$audioPlayer->play(‘vlc’,’away.vlc’);

$audioPlayer->play(‘avi’,’me.avi’);

收藏
分享
海报
0 条评论
199
上一篇:喝凉白开还是桶装水好(经常喝桶装水好吗) 下一篇:孕妇不孕吐正常吗(孕妇不孕吐的原因分析)

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码