<?php
//代理设计模式构建了透明置于两个不同对象之内的一个对象,从而能够截取或代理这两个对象间的通信或访问。
class CD{
protected $_title = '';
protected $_band = '';
protected $_handle = '';
public function __construct($title, $band){
$this->_title = $title;
$this->_band = $band;
}
public function buy(){
$this->_connect();
echo $this->_handle.": UPDATE CDs SET bought=1 WHERE band='".$this->_band."' AND title='".$this->_title."".PHP_EOL;
}
protected function _connect(){
$this->_handle = 'beijing';
}
}
$externalTitle = 'zuixuanminzufeng';
$externalBand = 'fenghuangchuanqi';
$cd = new CD($externalTitle, $externalBand);
$cd->buy();
echo PHP_EOL;
class ShandongProxy extends CD{
protected function _connect(){
$this->_handle = 'shandong';
}
}
$cd = new ShandongProxy($externalTitle, $externalBand);
$cd->buy();
//在需要截取两个对象之间的通信时,最佳的做法是使用一个基于代理设计模式的新对象。