代理模式

来源:互联网 发布:中美蜜月知乎 编辑:程序博客网 时间:2024/06/17 05:46
<?php// 代理模式function echoLine($msg) {echo $msg, '<br/>';}interface Sourceable{function method();}// 被代理对象class Source implements Sourceable{public function method() {echoLine('call method');}}// 代理对象class Proxy implements Sourceable{private $sourceable = null;// 代理关系在编译时确定public function __construct() {$this->sourceable = new Source();}// 动态设置代理对象public function setTarget(Sourceable $s) {$this->sourceable = $s;}public function method() {echoLine('before proxy!');$this->sourceable->method();echoLine('after proxy!');}}// test code$proxy = new Proxy();$proxy->method();$s = new Source();$proxy->setTarget($s);$proxy->method();

0 0
原创粉丝点击