php设计模式 Delegation(委托模式)

yipeiwu_com5年前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();
?>

相关文章

phpinfo的知识点总结

phpinfo是一个运行指令,为显示php服务器的配置信息。 phpinfo-输出大量PHP信息 bool phpinfo() 输出 PHP 当前状态的大量信息,包含了 PHP 编...

php中使用preg_replace函数匹配图片并加上链接的方法

介绍:preg_replace 执行正则表达式的搜索和替换,如果只是单纯的匹配字符串建议使用str_replace(),因为其执行效率高的多。mixed preg_replace ( m...

PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT

PHP屏蔽蜘蛛访问代码及常用搜索引擎的HTTP_USER_AGENT

PHP屏蔽蜘蛛访问代码代码: 常用搜索引擎名与 HTTP_USER_AGENT对应值 百度baiduspider 谷歌googlebot 搜狗sogou 腾讯SOSOsosospide...

php事务回滚简单实现方法示例

本文实例讲述了php事务回滚简单实现方法。分享给大家供大家参考,具体如下: $servername="localhost"; $username="root"; $password=...

php下封装较好的数字分页方法

复制代码 代码如下:/** * 获取页码导航HTML * @param $pageNum:当前页码 * @param $pageSize:每页数量 * @param $rowCount:...