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

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

相关文章

php $_ENV为空的原因分析

但有些朋友的$_ENV是空的,可能是原因是: 你的php.ini的variables_order值为"GPCS",也就是说系统在定义PHP预定义变量时的顺序是GET,POST,COOKI...

php中计算中文字符串长度、截取中文字符串的函数代码

在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启。当然,如果是...

PHP 通过Socket收发十六进制数据的实现代码

最近在php下做关于Socket通讯的相关内容,发现网络上好多人在了解如何进行16进制收发,研究了下,代码如下,欢迎拍砖。复制代码 代码如下:<?php  &n...

利用PHP扩展vld查看PHP opcode操作步骤

首先下载最新版vld扩展: 复制代码 代码如下: ~/public_html/php-5.3.13/ext> wget http://pecl.php.net/get/vld-0....

php 中文处理函数集合

--- 空格 --- string GBspace(string) --------- 每个中文字之间加空格 string GBunspace(string) ------- 每个中文字...