<?php
class Fruit
{
function callFruit()
{
print "Generate an Apple";
}
}
class FruitDelegator
{
private $targets;
function __construct()
{
$this->target[] = new Fruit();
}
function __call($name, $args)
{
foreach ($this->target as $obj)
{
$r = new ReflectionClass($obj);
if ($method = $r->getMethod($name))
{
if ($method->isPublic() && !$method->isAbstract())
{
return $method->invoke($obj, $args);
}
}
}
}
}
$obj = new FruitDelegator();
$obj->callFruit();
// 运行结果
// Generate an Apple
?>
<?php
class Color
{
function callColor()
{
print "Generate Red";
}
}
class ColorDelegator
{
private $targets;
function addObject($obj)
{
$this->target[] = $obj;
}
function __call($name, $args)
{
foreach ($this->target as $obj)
{
$r = new ReflectionClass($obj);
if ($method = $r->getMethod($name))
{
if ($method->isPublic() && !$method->isAbstract())
{
return $method->invoke($obj, $args);
}
}
}
}
}
$obj = new ColorDelegator();
$obj->addObject(new Color());
$obj->callColor();
?>
<?php
//使用委托模式之前,调用cd类,选择cd播放模式是复杂的选择过程
class cd {
protected $cdInfo = array();
public function addSong($song) {
$this->cdInfo[$song] = $song;
}
public function playMp3($song) {
return $this->cdInfo[$song] . '.mp3';
}
public function playMp4($song) {
return $this->cdInfo[$song] . '.mp4';
}
}
$oldCd = new cd;
$oldCd->addSong("1");
$oldCd->addSong("2");
$oldCd->addSong("3");
$type = 'mp3';
if ($type == 'mp3') {
$oldCd->playMp3();
} else {
$oldCd->playMp4();
}
<?php
namespace Tools;
/*
委托模式
去除核心对象中的判决和复杂功能性
*/
//委托接口
interface Delegate{
public function playList($list,$song);
}
//mp3处理类
class mp3 implements Delegate{
public function playList($list,$song){
return $list[$song].'.mp3';
}
}
//mp4处理类
class mp4 implements Delegate{
public function playList($list, $song)
{
return $list[$song].'.mp4';
}
}
class cdDelegate{
protected $cdInfo = array();
public function addSong($song){
$this->cdInfo[$song] = $song;
}
public function play($type,$song){
$name = '\Tools\\'.$type;
$obj = new $name;
return $obj->playList($this->cdInfo,$song);
}
}
$newCd = new cdDelegate();
$newCd->addSong("1");
$newCd->addSong("2");
$newCd->addSong("3");
echo $newCd->play('mp3','1');//只要传递参数就能知道需要选择何种播放模式
<?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();
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有