代理模式(Proxy):在客戶端與實體之間創建一個代理對象(proxy),客戶端對實體進行操做所有委派給代理對象,隱藏實體的具體實現細節。php
/** * Subject.php * Interface Subject */ interface Subject { public function request(); } /** * RealSubject.php * Class RealSubject */ class RealSubject implements Subject { /** * 真實請求方法具體實現 */ public function request() { // TODO: Implement request() method. echo "真實的請求<br/>\n"; } } /** * Proxy.php * Class Proxy */ class Proxy implements Subject { /** * @var RealSubject */ protected $realSubject; /** * Proxy constructor. */ public function __construct() { $this->realSubject = new RealSubject(); } /** * 調用真實請求方法 */ public function request() { // TODO: Implement request() method. $this->realSubject->request(); } }
客戶端調用:this
$proxy = new Proxy(); $proxy->request();