php设计模式 Delegation(委托模式)
程序员文章站
2023-02-26 15:52:21
复制代码 代码如下:
<?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
/**
* 委托模式 示例
*
* @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();
?>
上一篇: 绿色有什么不好的