当前位置: 网学 > 编程文档 > PHP > 正文

PHP设计模式之代理模式的深入解析

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/06/18
as the actual loading of the BLOB.
*/
class ImageProxy extends AbstractImage
{
public function __construct($path)
{
$this->_path = $path;
list ($this->_width, $this->_height) = getimagesize($path);
}

/**
* Creates a RawImage and exploits its functionalities.
*/
protected function _lazyLoad()
{
if ($this->_realImage === null) {
$this->_realImage = new RawImage($this->_path);
}
}

public function dump()
{
$this->_lazyLoad();
return $this->_realImage->dump();
}
}

/**
* Client class that does not use the data dump of the image.
* Passing blindly a Proxy to this class and to other Clients makes sense
* as the data would be loaded anyway when Image::dump() is called.
*/
class Client
{
public function tag(Image $img)
{
return '';
}
}

$path = ''/home/giorgio/shared/Immagini/kiki.png'';
$client = new Client();

$image = new RawImage($path); // loading of the BLOB takes place
echo $client->tag($image), "\n";

$proxy = new ImageProxy($path);
echo $client->tag($proxy), "\n"; // loading does not take place even here

以上代码实现了PHP的代理模式。简单来讲,代理模式就是为其他对象提供一个代理以控制对这个对象的访问。

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号