<?php/*** Created by PhpStorm.* User: insgeek* Date: 2017年2月21日* Time: 15:51* 适配器模式*/namespace DesignPattern\AdapterPattern;interface MediaPlayer{public function play(string $audioType, string $fileName);}interface AdvancedMediaPlayer{public function playVlc(string $fileName);public function playMp4(string $fileName);}class VlcPlayer implements AdvancedMediaPlayer{public function playVlc(string $fileName){// TODO: Implement playVlc() method.print_r("Playing vlc file. Name: " . $fileName . PHP_EOL);}public function playMp4(string $fileName){// TODO: Implement playMp4() method.}}class Mp4Player implements AdvancedMediaPlayer{public function playVlc(string $fileName){// TODO: Implement playVlc() method.}public function playMp4(string $fileName){// TODO: Implement playMp4() method.print_r("Playing mp4 file. Name: " . $fileName . PHP_EOL);}}class MediaAdapter implements MediaPlayer{public $advancedMusicPlayer;public function __construct(string $audioType){if (strcasecmp($audioType, 'vlc') === 0) {$this->advancedMusicPlayer = new VlcPlayer();} elseif (strcasecmp($audioType, 'mp4') === 0) {$this->advancedMusicPlayer = new Mp4Player();}}public function play(string $audioType, string $fileName){// TODO: Implement play() method.if (strcasecmp($audioType, 'vlc') === 0) {$this->advancedMusicPlayer->playVlc($fileName);} elseif (strcasecmp($audioType, 'mp4') === 0) {$this->advancedMusicPlayer->playMp4($fileName);}}}class AudioPlayer implements MediaPlayer{public $mediaAdapter;public function play(string $audioType, string $fileName){// TODO: Implement play() method.if (strcasecmp($audioType, 'mp3') === 0) {print_r("Playing mp3 file. Name" . $fileName . PHP_EOL);} elseif (strcasecmp($audioType, 'vlc') === 0 || strcasecmp($audioType, 'mp4') === 0) {$this->mediaAdapter = new MediaAdapter($audioType);$this->mediaAdapter->play($audioType, $fileName);} else {print_r("Invalid media {$audioType} format not supported" . PHP_EOL);}}}$audioPlayer = new AudioPlayer();$audioPlayer->play('mp3', 'some one like you.mp3');$audioPlayer->play('mp4', 'see you again.mp4');$audioPlayer->play('ppt', 'test.ppt');
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。