Design Patterns Reference: Proxy

The Proxy Design Pattern buils an object that is positioned transparently within two other objects in order to intercept or proxy the communication or access.

 

class CD
{
protected $_title = ”;
protected $_band = ”;
protected $_handle = null;

 

public function __construct($title, $band)
{
$this->_title = $title;
$this->_band = $band;
}

 

public function buy()
{
$this->_connect();

 

$query = “update CDs set bought=1 where band='”;
$query .= mysql_real_escape_string($this->_band, $this->_handle);
$query .= “‘ and title='”;
$query .= mysql_real_escape_string($this->_title, $this->_handle);
$query .= “‘”;

 

mysql_query($query, $this->_handle);
}

 

protected function __connect()
{
$this->_handle = mysql_connect(‘localhost’, ‘user’, ‘pass’);
mysql_select_db(‘CD’, $this->_handle);
}
}

 

// CD class simply takes a band and title, updates the DB, and completes the sale
$externalTitle = “Waste of a Rib”;
$externalBand = ‘Never Again’;

 

$cd = new CD($externalTitle, $externalBand);
$cd->buy();

 

// A new store in another city will leverage this now via Proxy
// simply by overwriting the protected connect method of the CD class

class DallasNOCCDProxy extends CD
{
protected function __connect()
{
$this->_handle = mysql_connect(‘dallas’, ‘user’,’pass’);
mysql_select_db(‘CD’);
}
}

 

$externalTitle = “Waste of a Rib”;
$externalBand = ‘Never Again’;

 

$cd = new DallasNOCCDProxy($externalTitle, $externalBand);
$cd->buy();

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

 

 

Leave a Reply

You must be logged in to post a comment.