@Chiang
2020-01-03T10:58:02.000000Z
字数 1415
阅读 552
设计模式
- 抽象主题接口
- 真实主题类
- 代理角色
<?php
/**
* Subject.php
*
* @author Luffy <lufei@swoole.com>
* @date 2019/12/16
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Proxy;
interface Subject
{
public function get();
public function put();
}
<?php
/**
* RealSubject.php
*
* @author Luffy <lufei@swoole.com>
* @date 2019/12/16
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Proxy;
class RealSubject implements Subject
{
public function get()
{
echo __CLASS__ . " get\n";
}
public function put()
{
echo __CLASS__ . " put\n";
}
}
<?php
/**
* Proxy.php
*
* @author Luffy <lufei@swoole.com>
* @date 2019/12/16
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Proxy;
class Proxy
{
/**
* @var RealSubject
*/
protected $realSubject;
/**
* Proxy constructor.
*/
public function __construct()
{
$this->realSubject = new RealSubject();
}
public function __call($name, $arguments)
{
return $this->realSubject->{$name}(...$arguments);
}
// public function get()
// {
// $this->realSubject->get();
// }
public function send()
{
echo __CLASS__ . " send\n";
}
}
<?php
/**
* Client.php
*
* @author Luffy <lufei@swoole.com>
* @date 2019/12/16
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Proxy;
include __DIR__ . '/../vendor/autoload.php';
class Client
{
public static function run()
{
$proxy = new Proxy();
$proxy->get();
$proxy->put();
$proxy->send();
}
}
Client::run();