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

相关文章

php时间函数用法分析

本文实例讲述了php时间函数用法。分享给大家供大家参考,具体如下: php中有unix时间戳的 相关操作函数,使用很方便 time() 返回当前的 Unix 时间戳 microtime...

php发送html格式文本邮件的方法

本文实例讲述了php发送html格式文本邮件的方法。分享给大家供大家参考。具体实现方法如下: <?php $to = "simon@mailexample.com,...

Php获取金书网的书名的实现代码

复制代码 代码如下:<?php $url="http://www.golden-book.com/booksinfo/12/264.html"; $lines_array = fi...

php抽奖概率算法(刮刮卡,大转盘)

本文实例为大家分享了php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法,用法很简单,代码里有详细注释说明,供大家参考,具体内容如下 <?php /* * 经典的概率算...

PHP与Perl之间知识点区别整理

什么是Perl? Perl是一种动态的,高级的、通用的编程语言,它没有任何官方缩写。它是纯粹使用C编程语言开发和实现的;它支持跨平台操作系统;它是根据GNU通用公共许可证授权的。它具有不...