<?php
// 主题接口
interface Subject
{
public function request();
}
// 真实主题类
class RealSubject implements Subject
{
public function request()
{
echo “RealSubject handles request.\n”;
}
}
// 代理类
class Proxy implements Subject
{
private $realSubject;
public function __construct(RealSubject $realSubject)
{
$this->realSubject=$realSubject;
}
public function request()
{
if ($this->checkAccess()) {
$this->realSubject->request();
$this->logAccess();
}
}
private function checkAccess()
{
echo “Proxy checks access.\n”;
return true;
}
private function logAccess()
{
echo “Proxy logs access.\n”;
}
}
// 客户端代码
$realSubject=new RealSubject();
$proxy=new Proxy($realSubject);
$proxy->request();
// 主题接口
interface Subject
{
public function request();
}
// 真实主题类
class RealSubject implements Subject
{
public function request()
{
echo “RealSubject handles request.\n”;
}
}
// 代理类
class Proxy implements Subject
{
private $realSubject;
public function __construct(RealSubject $realSubject)
{
$this->realSubject=$realSubject;
}
public function request()
{
if ($this->checkAccess()) {
$this->realSubject->request();
$this->logAccess();
}
}
private function checkAccess()
{
echo “Proxy checks access.\n”;
return true;
}
private function logAccess()
{
echo “Proxy logs access.\n”;
}
}
// 客户端代码
$realSubject=new RealSubject();
$proxy=new Proxy($realSubject);
$proxy->request();
© 版权声明
文章版权归作者所有,未经允许请勿转载。