参考:
http://www.lai18.com/content/373831.html
《PHP设计模式》作 者:(美)萨莱 译 者:梁志敏,蔡建
定义:
通过分配或委托至其他对象,委托设计模式能够去除可细心对象中的判决和复杂的功能性。
优点:
这种方式是基对象能够简单,动态地创建和访问任何委托者。
适用场景:
为了去除核心对象的复杂性并且能够动态添加新的功能,就应当使用委托设计模式。
<?php
/**
* 委托模式 示例
* @create_date: 2010-01-04
*/
class PlayList
{
var $_songs = array();
var $_object = null;
function PlayList($type) {
$object = $type."PlayListDelegation";
$this->_object = new $object();
}
function addSong($location,$title) {
$this->_songs[] = array("location"=>$location,"title"=>$title);
}
function getPlayList() {
return $this->_object->getPlayList($this->_songs);
}
}
class mp3PlayListDelegation {
function getPlayList($songs) {
$aResult = array();
foreach($songs as $key=>$item) {
$path = pathinfo($item['location']);
if(strtolower($item['extension']) == "mp3") {
$aResult[] = $item;
}
}
return $aResult;
}
}
class rmvbPlayListDelegation {
function getPlayList($songs) {
$aResult = array();
foreach($songs as $key=>$item) {
$path = pathinfo($item['location']);
if(strtolower($item['extension']) == "rmvb") {
$aResult[] = $item;
}
}
return $aResult;
}
}
$oMP3PlayList = new PlayList("mp3");
$oMP3PlayList->getPlayList();
$oRMVBPlayList = new PlayList("rmvb");
$oRMVBPlayList->getPlayList();
?>